1

Here's my situation: I'm working with an array that has a bunch of nested objects. One of those nested objects has a name that changes dynamically. How can I consistently access a key of one of it's child objects?

Here's a few examples of the paths that it can have:

kml[id].overlayData._layers.136._bounds._northEast.lat
kml[id].overlayData._layers.143._bounds._northEast.lat
kml[id].overlayData._layers.82._bounds._northEast.lat
kml[id].overlayData._layers.87._bounds._northEast.lat
kml[id].overlayData._layers.76._bounds._northEast.lat

The child object that I'm trying to access is 'lat', which has a couple of keys within it.

Apologies for any weird / incorrect use of terminology, I'm still trying to get my head around concepts like this.

5
  • what part is changing? Commented Oct 26, 2016 at 17:16
  • Do you know what the number of the object you want to access is? Where is that stored? Commented Oct 26, 2016 at 17:17
  • @NinaScholz The number in-between '_layers' and '_bounds'. Commented Oct 26, 2016 at 17:17
  • @Feathercrown I've edited the question to try and clarify a bit more . Commented Oct 26, 2016 at 17:20
  • is there only one property or more than one unknown? Commented Oct 26, 2016 at 17:32

3 Answers 3

1

You could get the key with Object.keys.

var keys = Object.keys(kml[id].overlayData._layers);

// access
keys.forEach(function (key) {
    // kml[id].overlayData._layers[key]._bounds._northEast.lat
});

For only one unknown key, you could use directly the first element of the keys array

var key = Object.keys(kml[id].overlayData._layers)[0];

// access with
kml[id].overlayData._layers[key]._bounds._northEast.lat
Sign up to request clarification or add additional context in comments.

4 Comments

This only gets the first key; additionally, it can be a different key every time!
i do not understand you. the question was, how to access a property without knowing the name. in your answer, you know the name, but that is not the question, as i read it. please add a less complicated solution.
Our answers are both correct and flawed for different interpretations of the question.
Thanks @NinaScholz, you gave the best answer to my admittedly poorly worded question! However, am I right in thinking that Object.keys won't return keys that have been marked as 'enumerable: false'?
0

Try this, if I understand the question correctly:

var number = 354; //whatever number you want to get the value from
kml[id].overlayData._layers['number']._bounds._northEast.lat

Note that you can't use blahblah.123 to access a number-started property; use blahblah['123'] instead. This also allows you to pass in variables, like the example above.

Comments

0

You can loop thanks to for..in loop

for (var prop in kml[id].overlayData._layers) 
   console.log(kml[id].overlayData._layers[prop]._bounds._northEast.lat);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.