简体   繁体   中英

How to get first grand child of an array inside of array

I have an array with 10 elements. Each element is also an array and each of their children are objects.

Lets call it "array".

array[0] gives me the first element. It is filled with 20 objects.

I want to get the first one, and then a value in that object, such as this:

var id = array[0].getFirstElement().id;

How do I do it?

I tried array[0][0] and other things but didn't work.

For this example

array = [[{id:2, ...}, ...], ...]

You need array[0][0].id

See a for in loop: here

for you something like this may work (not tested)

for(var prop in array[0]){
  if( array[0].hasOwnProperty( prop ) ) {
    console.log("array[0]." + prop.id + " = " + array[0][prop].id);
  } 
  //and because you only want the first
  break;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM