0

I need to access an array of feature attributes that is nested inside an item list object. It looks like this.

var itemsList = {
    _searchedFeatures: {
      Wildlife Management Areas: [{}, {}, {}]
    }
}

I need to access the Wildlife Management Areas(WMA) array and loop through it, extract an attribute value from each feature in the array and push that value to another array. The problem I am having is that I cannot access the WMA array.

I have tried using a combination of dot notation and bracket notation to get to the WMA array, but I always get undefined. I know the WMA is there and contains a list of objects because I can see them when I access the _searchFeatures object.

I have tried

itemList._searchedFeatures

which returns the array. I can see the array in the chrome console, but I need to get to the values inside or the array.

Chrome console: Searched Features

{}
  Wildlife Management Areas: (5) [c, c, c, c, c] //I need to get to this 
  array!

This code is not working:

itemsList._searchedFeatures["Wildlife Management Areas"]; 

I am expecting to see an indexed list of features that looks something like this:

0: {...}
1: {...}
2: {...}

But instead, I just get undefined. What is interesting, is when I hover the mouse over Wildlife Management Areas in the console, I am seeing what looks to be the JavaScript dot notation solution, but I don't know how to duplicate it. This is what I am seeing:

[""Wildlife Management Areas""]

I have never seen double double quotes and this will obviously not work, but I have no idea how to interpret this. Please let me know what I am not understanding.

10
  • 1
    This shouldn't work because it's not valid syntax, you should be seeing "unexpected identifier" in the console, because without quotes around Wildlife Management Areas it's not a valid property Apr 8, 2019 at 15:31
  • 1
    can you post the output of console.log(JSON.stringify(itemsList));
    – BlackPearl
    Apr 8, 2019 at 15:32
  • @BlackPearl easy to test that in your own console -- as expected, it will throw a Syntax Error with or without JSON.stringify Apr 8, 2019 at 15:33
  • @SterlingArcher I think BlackPearl wants to know exactly what the structure of the object is, without all the console cruft. Apr 8, 2019 at 15:35
  • You may be trying to get the property before it is there. console.log will show the object as it is at the time you interact with it in the console, not as it was when it hit the console.log statement in the code.If you use @BlackPearl's method of logging, just before you try to get the property, you may see something different. Apr 8, 2019 at 15:37

2 Answers 2

0

This works:

setTimeout(function() {
    var wmaArray = itemList._searchedFeatures['wildlife Management Areas'];
    console.log("WMAs ", wmaArray);
}, 2000);

It's just taking a long time to get the results, so I had to extend the timeout. I know it's not the best way, I do want to set up a promise to do this, but I am still learning javascript and not very good at those yet!

-1

You should put Wildlife Management Areas in quotes for this to work

var itemsList = {
    _searchedFeatures: {
        'Wildlife Management Areas': [{}, {}, {}]
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.