简体   繁体   English

如何在数组内部获取数组的第一个子代

[英]How to get first grand child of an array inside of array

I have an array with 10 elements. 我有一个包含10个元素的数组。 Each element is also an array and each of their children are objects. 每个元素也是一个数组,每个子元素都是对象。

Lets call it "array". 让我们称之为“数组”。

array[0] gives me the first element. array [0]给我第一个元素。 It is filled with 20 objects. 它充满了20个对象。

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. 我尝试了array [0] [0]和其他方法,但是没有用。

For this example 对于这个例子

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

You need array[0][0].id 您需要array[0][0].id

See a for in loop: here 请参阅for循环: 此处

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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