...

View Full Version : Assigning things inside braces to a variable?



Scriptdaemon
12-11-2006, 03:36 AM
I've seen this in some code, so could sometime tutor me on this exactly and how I would use this, assuming it's not something that is parsed somewhere else in the code I took this from?

Example:

var a = {pid:1, host:1, hl:1}

liorean
12-11-2006, 04:32 AM
It's quite simple: The {name0:value0,name0:value0, ... ,namen:valuen} syntax defines an object, and that object is given the property name0 with a value of value0 and so on. These properties can be accessed in the same way that properties of any other object can be accessed.


var
obj={
string:'b{l{ar}g}h',
number:42,
boolean:true,
object:{},
'function':function(m,p){alert(m);return p;},
array:[],
regex:/\{(.*?)\}/gi};

// ... has the same meaning as...

var
obj=new Object;
obj.string='b{l{ar}g}h';
obj.number=42;
obj.boolean=true;
obj.object=new Object;
obj['function']=function(m,p){alert(m);return p;};
obj.array=new Array;
obj.regex=/\{(.*?)\}/gi;This is hugely useful for more complex code, because objects are a good way of keeping track of things that belong together. It can also be used to get around the fact that some of JavaScript's values are primitive types and some are referential types, by wrapping stuff in a referential type. As such, it is a very useful construct for structuring your code.

This syntax is also the basis of the JSON data format.

Scriptdaemon
12-11-2006, 04:44 AM
This is exactly what I need for what I want to do then, thanks.

So, if I understand correctly, I'd be able to do this?

var list = {id: id, name: name, title: title};
var id = list.id;
var name = list.name;
var title = list.title;

Then use the three variables (id, name, and title) for use in later code?

Oh, and would this also be allowed in an array? Such as:

var rows = new Array({id: "text", name: "text", title: "text"}, {id: "text", name: "text", title: "text"}, {id: "text", name: "text", title: "text"}, {id: "text", name: "text", title: "text"});

liorean
12-11-2006, 12:35 PM
So, if I understand correctly, I'd be able to do this?

var list = {id: id, name: name, title: title};
var id = list.id;
var name = list.name;
var title = list.title;

Then use the three variables (id, name, and title) for use in later code?Of course. Or you could just use list.id, list.name and list.title directly. You can even use
with(list){
doSomethingWithId(id);
doSomethingWithName(name);
doSomethingWithTitle(title);
}if you want.


WARNING!
In general, the with statement should be avoided until you have a clear picture of how it affects variable scope resolution and object members lookups. It can mess program performance up something fiercely, and it can also make programs behave in unexpected ways.




Oh, and would this also be allowed in an array? Such as:

var rows = new Array({id: "text", name: "text", title: "text"}, {id: "text", name: "text", title: "text"}, {id: "text", name: "text", title: "text"}, {id: "text", name: "text", title: "text"});Yes, that too. However, similarly to how you can use {} instead of new Object, I would suggest you to use [] instead of new Array. It looks cleaner, and you won't have to deal with the following inconsistency:
var
arr0=[4,3], // => [4,3]
arr1=new Array(4,3), // => [4,3]
arr2=[4], // => [4]
arr3=new Array(4); // => [,,,,]

Scriptdaemon
12-12-2006, 12:21 AM
Thanks again, I shall do that with the arrays then.

Scriptdaemon
12-12-2006, 12:50 AM
Another question I just thought of, am I able to order the array by the name value of the object?

For example, if I have this:

var rows = [{id: "text", name: "a"}, {id: "text", name: "z"}, {id: "text", name: "g"}, {id: "text", name: "b"}];

How would I make it so if I use it in a for loop to put them in order on the page so that it shows:

a
b
g
z

liorean
12-12-2006, 10:45 AM
There's a built in sorting functions of arrays. It takes a function as it's argument. When sorting it uses that function to compare things so that it can determine what order to place them in. For example:
var
i,
l,
original=[
{id: "text", name: "a"},
{id: "text", name: "z"},
{id: "text", name: "g"},
{id: "text", name: "b"}];
sorted=[].concat(original).sort(compareByName);

function compareByName(a,b){
var
c=a.name,
d=b.name;
if(c<d)
return -1;
if(c>d)
return 1;
return 0;
}

for(i=0,l=original.length;i<l;i++)
alert('original['+i+'].name: '+original[i].name);
for(i=0,l=sorted.length;i<l;i++)
alert('sorted['+i+'].name: '+sorted[i].name);
One thing to note about the sort method is that it modifies the array in place. The whole reason I did [].concat(original).sort(...) is to make a copy of the array. If it doesn't matter to you that the order of the original array is changed, then you can simply use original.sort(...).

Scriptdaemon
12-12-2006, 04:05 PM
I understand for the most part, but am confused by some of it. Where do the a and b variables come from? I don't see you assigning them anything when you call the function.

And, I'm also not familiar with concat. What exactly does that do? Simply copy the array, and that's it?

liorean
12-12-2006, 06:10 PM
I understand for the most part, but am confused by some of it. Where do the a and b variables come from?Ah, that isn't entirely obvious, I know. Remember that I told you that sort takes a function to do the comparing? The way sort works, it will call the comparison function with two arguments for each time it wants to compare a value with another. So, the a and b arguments comes from the sort function.
I don't see you assigning them anything when you call the function.....and now you know why.
And, I'm also not familiar with concat. What exactly does that do? Simply copy the array, and that's it?concat means concatenate. It simply adds all the elements from one array onto another array. So in effect [].concat(arr) will add all the elements from arr to an empty array, effectively making a copy of arr. Note that it is a shallow copy and not a deep copy, however.

Scriptdaemon
12-13-2006, 05:11 AM
Alright, so I did that and now understand all that, but I tried ordering one property within another. Example, I want to sort the title within the name.

var rows = [{id: "id1", name: "name1", title: "title1", cds: 1, dvds: 0},
{id: "id2", name: "name2", title: "title2", cds: 0, dvds: 2},
{id: "id4", name: "name3", title: "title3", cds: 1, dvds: 1},
{id: "id2", name: "name2", title: "title2", cds: 0, dvds: 2},
{id: "id3", name: "name3", title: "title3", cds: 1, dvds: 1},
{id: "id1", name: "name1", title: "title1", cds: 1, dvds: 0},
{id: "id2", name: "name2", title: "title2", cds: 0, dvds: 2},
{id: "id3", name: "name3", title: "title3", cds: 1, dvds: 1},
{id: "id1", name: "name1", title: "title1", cds: 1, dvds: 0},
{id: "id2", name: "name2", title: "title2", cds: 0, dvds: 2},
{id: "id3", name: "name8", title: "title3", cds: 1, dvds: 1},
{id: "id1", name: "name1", title: "title1", cds: 1, dvds: 0},
{id: "id2", name: "name2", title: "title2", cds: 0, dvds: 2},
{id: "id3", name: "name3", title: "title1", cds: 1, dvds: 1},
{id: "id2", name: "name2", title: "title2", cds: 0, dvds: 2},
{id: "id3", name: "name7", title: "title0", cds: 1, dvds: 1},
{id: "id1", name: "name1", title: "title1", cds: 1, dvds: 0},
{id: "id2", name: "name2", title: "title2", cds: 0, dvds: 2},
{id: "id3", name: "name8", title: "title3", cds: 1, dvds: 1},
{id: "id1", name: "name1", title: "title5", cds: 1, dvds: 0},
{id: "id2", name: "name2", title: "title2", cds: 0, dvds: 2},
{id: "id3", name: "name3", title: "title3", cds: 1, dvds: 1},
{id: "id1", name: "name1", title: "title1", cds: 1, dvds: 0},
{id: "id2", name: "name2", title: "title2", cds: 0, dvds: 2},
{id: "id3", name: "name3", title: "title3", cds: 1, dvds: 1}];

So that the entire thing would be sorted by the name, and then within that would be sorted by the title.

I'll try messing with it some more, but so far I haven't quite figured it out.

Scriptdaemon
12-13-2006, 05:34 AM
Scratch that. It was a lot easier than I thought.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum