简体   繁体   English

如何在Javascript中获取subArray的所有子级

[英]How to get all children of a subArray in Javascript

So here's my problem: 所以这是我的问题:

So this is what my array looks like, I have shown a fragment of it here (from the console window). 这就是我的数组的样子,我在这里(从控制台窗口)显示了它的一部分。

数组

It's overall pretty basic right? 总体来说很基本吧? Except for it's indexing. 除了它的索引。 As you see the first has value "1" and let's say the second has value "4" . 如您所见,第一个的值为"1" ,第二个的值为"4" As for it's subarrays, these have custom indexes too. 至于子数组,它们也具有自定义索引。

Therefore, the old fashioned: 因此,老式的:

for(i=0; i<array.length; i++){
  someVar = array[i];
  //do something
}

won't work. 将无法正常工作。

I get the 1 or 4 from iterating through another array, so don't need to get those. 我通过迭代另一个数组得到1或4,所以不需要获取它们。

I want to get all subArrays(not the further nested subArrays, only the second level). 我想获取所有subArrays(不是进一步嵌套的subArrays,仅是第二层)。

So basicly what I want is something like this(is there something like this?): 所以基本上我想要的是这样的(是否有这样的东西?):

array[someIndex].children.forEach(
  //do something with **this**
)

To make it more practical for you: So I know that the first array has index 1 . 为了让您更实用:我知道第一个数组的index 1 How can I get the values of cat1 and cat2 without knowing it has index 2 (this could also be 6 or 42 for example). 如何在不知道cat1cat2具有index 2情况下获取它们的值( cat1 ,它也可以是642 )。 In this case, the first array only has one subArray but I would like to make it work for multiple subArrays. 在这种情况下,第一个数组只有一个subArray,但我想使其适用于多个subArray。 (to clarify, select cat1 , cat2 from the second level subarray with, in this case, index 2 ) (为澄清cat1 ,从第二级子cat2选择cat1cat2 ,在这种情况下为index 2

Thanks in advance, any help is appreciated! 在此先感谢您的帮助!

Evoc 回声

Those aren't arrays. 这些不是数组。 You have 你有

[   {   "1": { etc...

which is an array containing an object containing multiple other objects. 这是一个包含对象的数组,该对象包含多个其他对象。 You can't really use a for(i=...) loop for this, because your keys aren't sequential. 您实际上不能for(i=...)使用for(i=...)循环,因为您的键不是顺序的。 You should use a for ... in loop instead: 您应该改为使用for ... in循环:

arr = [{"1":{....}}];

for (i in arr[0]) {
   for (j in arr[0][i]) {
       console.log(arr[0][i][j]['msg']);
   }
}

Use Object.getOwnPropertyNames to get the properties ordered with the integer indices first, from smallest to greatest. 使用Object.getOwnPropertyNames可以获取从小到大从小到大的顺序。 Iterate them to get msg1 . 迭代它们以获得msg1

 var array = {"1":{"3":{"5":{"data":"someData","moreData":"someMoreData"},"msg1":"hello world","msg2":"foo equals bar"},"5":{"8":{"data":"someData","moreData":"someMoreData"},"msg1":"world hello","msg2":"bar equals foo"},"your_name":"jimmy","your_msg":"hello world"},"4":{"3":{"5":{"data":"someData","moreData":"someMoreData"},"msg1":"hello world","msg2":"foo equals bar"},"5":{"8":{"data":"someData","moreData":"someMoreData"},"msg1":"world hello","msg2":"bar equals foo"},"your_name":"billy","your_msg":"foo equals bar"}}; for(let key of Object.getOwnPropertyNames(array[1])) { if(Object(array[1][key]) !== array[1][key]) break; console.log('msg1 of "'+key+'": ' + array[1][key].msg1); } console.log('your_msg: ' + array[1].your_msg); 

The example you showed it's an array with only one index, inside of this index there are nested objects, you could iterate them using the property iterator (foreach). 您显示的示例是一个只有一个索引的数组,该索引内有嵌套对象,您可以使用属性迭代器(foreach)对其进行迭代。 You have to go to the second level tho, since the values you need are there. 由于您需要的值在那里,因此您必须进入第二级。

for (var key in object) {
    for(var anotherKey in object[key]) {
        //check for undefined 
        if(object[key][anotherKey].hasOwnProperty('msg')) {
            //code
        }

    }
}

{ } - declaring objects (literal) {}-声明对象(文字)

[ ] - declaring arrays []-声明数组

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

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