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.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
GeoGene
  • 11
  • 4
  • 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 – Sterling Archer Apr 08 '19 at 15:31
  • 1
    can you post the output of console.log(JSON.stringify(itemsList)); – BlackPearl Apr 08 '19 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` – Sterling Archer Apr 08 '19 at 15:33
  • @SterlingArcher I think BlackPearl wants to know exactly what the structure of the object is, without all the console cruft. – Heretic Monkey Apr 08 '19 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. – Heretic Monkey Apr 08 '19 at 15:37
  • @BlackPearl I get a circular reference error when I try using JSON.stringify(itemList). I posted the structure from the console in the first code block. Also I am putting Quotes around "Wildlife Management Areas". – GeoGene Apr 08 '19 at 17:32
  • @HereticMonkey Yes, I was trying to get the property before it was there so I tried using a timeout which seems to work fine for getting the _searchedFeatures. But then when I try and get the "Wildlife Management Areas" it fails. – GeoGene Apr 08 '19 at 17:37
  • `setTimeout(function() { var wma = itemsList._searchedFeatures["Wildlife Management Areas"]; console.log("WMAs ", wma); }, 1000); – GeoGene Apr 08 '19 at 17:43
  • @GeoGene and the exact output for the above code is? – BlackPearl Apr 08 '19 at 17:47
  • And regarding the double double quotes in [""Wildlife Management Areas""], chrome console seems to indicate it this way whenever an object property has spaces. So, it should be accessible with your code. – BlackPearl Apr 08 '19 at 17:49

2 Answers2

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!

GeoGene
  • 11
  • 4
-1

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

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