Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    95
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Assigning things inside braces to a variable?

    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}

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.

    Code:
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New Coder
    Join Date
    Dec 2006
    Posts
    95
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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"});
    Last edited by Scriptdaemon; 12-11-2006 at 03:47 AM.

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by Scriptdaemon View Post
    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
    Code:
    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:
    Code:
    var
        arr0=[4,3], // => [4,3]
        arr1=new Array(4,3), // => [4,3]
        arr2=[4], // => [4]
        arr3=new Array(4); // => [,,,,]
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    New Coder
    Join Date
    Dec 2006
    Posts
    95
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks again, I shall do that with the arrays then.

  • #6
    New Coder
    Join Date
    Dec 2006
    Posts
    95
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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:
    Code:
    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(...).
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    New Coder
    Join Date
    Dec 2006
    Posts
    95
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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?
    Last edited by Scriptdaemon; 12-12-2006 at 03:14 PM.

  • #9
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by Scriptdaemon View Post
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #10
    New Coder
    Join Date
    Dec 2006
    Posts
    95
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.

  • #11
    New Coder
    Join Date
    Dec 2006
    Posts
    95
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Scratch that. It was a lot easier than I thought.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •