www.webdeveloper.com
Results 1 to 5 of 5

Thread: JavaScript Arrays

  1. #1
    Join Date
    Apr 2012
    Posts
    2

    JavaScript Arrays

    I have the following:

    var x = [];
    x.push({layerId: 2, layerName: 'Counties'});
    x.push({layerId: 3, layerName: 'States'});

    Now when setting up parameters, one of the properties is layerIds (which is an array). How can I set 'x' to layerIds?

    I tried this, but it doesn't work:

    layerIds = x.layerId;

  2. #2
    Join Date
    Nov 2010
    Posts
    1,036
    I'm probably not getting it, but is
    Code:
    x[0].layerId
    what you're looking for? (where 0 is the position in the array of the object whose property you want to access)

  3. #3
    Join Date
    Apr 2012
    Posts
    2
    No. I want all of the 'layerId' values in the 'x' array.

    If it was this:

    var x = [];
    x.push(2);
    x.push(3);

    and

    layerIds = x;

    then everything would be fine. But I have 'x' set up as the following:

    var x = [];
    x.push({layerId: 2, layerName: 'Counties'});
    x.push({layerId: 3, layerName: 'States'});

    and I want the layerIds array to be set to the layerId property of the 'x' array.

  4. #4
    Join Date
    Nov 2010
    Posts
    1,036
    well I guess you would have to push those values onto the layerIds array, preferably in a loop.

    to cut down on all that push repetition, you can construct the original array like this:

    var x = [{layerId: 2, layerName: 'Counties'},
    {layerId: 3, layerName: 'States'}];

    and once you have done with that, like I say, loop through the x array, pushing the layerId values onto your layerIds array

    although if you are using jQuery I think (but I don't know because I don't use it) that there are some built-in methods for cloning arrays and objects - maybe you can mix n match

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by dustwhirl View Post
    I have the following:

    var x = [];
    x.push({layerId: 2, layerName: 'Counties'});
    x.push({layerId: 3, layerName: 'States'});

    Now when setting up parameters, one of the properties is layerIds (which is an array). How can I set 'x' to layerIds?

    I tried this, but it doesn't work:

    layerIds = x.layerId;
    Try:
    Code:
    var layerIds=[];
    for(var i=0;i<x.length;i++){
    layerIds[layerIds.length]=x[i].layerId;
    }

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