www.webdeveloper.com
Results 1 to 3 of 3

Thread: How to dynamically assign name to object array

  1. #1
    Join Date
    Jan 2013
    Posts
    2

    How to dynamically assign name to object array

    For example say if have an object array obArr,this object consists of Id,Name

    if I was to access the obArr[0].Id , this would give me back the Id value of thefirst object, but what would happen if I don't know what this object consists of as It could be dynamic is there anyway to dynamically call the Object 'attribute',

    The way I'm looking for is to have another array,
    var myArr =new Array("Id","Name");

    and call obArr[0].myArr[0] , instead of obArr[0].Id

    Is there a way to do this with Javascript?,

    Thanks.

  2. #2
    Join Date
    Dec 2012
    Posts
    170
    You can call all of the attributes in an object like this without knowing the keys.

    Code:
    for(key in obArr[0]) {
       console.log(obArr[0][key]);
    }
    If you just want the first attribute you could do something like this

    Code:
    for(key in obArr[0]) {
       var firstAtt = obArr[0][key];
       break;
    }

  3. #3
    Join Date
    Jan 2013
    Posts
    2
    iBeZi!,
    Thank you!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles