简体   繁体   English

如何遍历数组字典(不知道键名)

[英]How to iterate over a dictionary of arrays (without knowing the key name)

I have a an object that looks similar to this: 我有一个看起来与此类似的对象:

[
    {"key1": ["2019-04-12-14:54:29.190", "19", "0", "4325", "1"]},
    {"key2": ["2019-04-12-14:54:29.191", "20", "0", "2212", "1"]},
    {"key3": ["2019-04-12-14:54:29.192", "22", "0", "4376", "0"]}
]

However I do NOT know the names of the keys (ie key1, key2 and key3 names are not known to me). 但是我不知道键的名称(即,我不知道key1,key2和key3的名称)。

This post is almost exactly what I want, except that method requires you to know the names of the keys. 这篇文章几乎正是我想要的,除了该方法要求您知道键的名称。

I need to be able to iterate over the key name and it's value array. 我需要能够遍历键名及其值数组。

I've tried this: 我已经试过了:

for (var i in zk) {
    for (var j in zk[i]) {
        console.log(j)
    }
}

But that only prints the key names. 但这仅显示键名。 How can I iterate through the list as well? 我如何也可以遍历列表? In most langues iterating over j seems the logical choice but not in this case. 在大多数语言中,遍历j似乎是逻辑选择,但在这种情况下不是。 Any ideas? 有任何想法吗? Thank you. 谢谢。

Let's continue from the code sample you have provided. 让我们从您提供的代码示例继续。

1) Getting the keys of each element 1)获取每个元素的键

for (let i in zk) {
  for (let j in zk[i]) {
    console.log(j)
  }
}

2) Getting the list within each element 2)获取每个元素中的列表

for (let i in zk) {
  for (let j in zk[i]) {
    console.log(zk[i][j])
  }
}

3) Iterating through each list within each element 3)遍历每个元素中的每个列表

for (let i in zk) {
  for (let j in zk[i]) {
    for (let k = 0; k < zk[i][j].length; k++) {
      console.log(zk[i][j][k])
    }
  }
}

Alternatively, you can use Object.values , which returns the values of each key-value pair. 另外,您可以使用Object.values ,它返回每个键值对的值。 You can consider using Array.map() too. 您也可以考虑使用Array.map()

for (let i in zk) {
  Object.values(zk[i]).map(list => {
    list.map(element => {
      console.log(element);
    });
  });
}

You could take a differetn approach for iterating the given arrays and objects, with 您可以采用另一种方法来迭代给定的数组和对象,

 var data = [{ key1: ["2019-04-12-14:54:29.190", "19", "0", "4325", "1"] }, { key2: ["2019-04-12-14:54:29.191", "20", "0", "2212", "1"] }, { key3: ["2019-04-12-14:54:29.192", "22", "0", "4376", "0"] }]; data.forEach((object, outerIndex) => Object.entries(object).forEach(([key, array]) => array.forEach((value, innerIndex) => console.log(outerIndex, key, innerIndex, value) ) ) ); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

You missed the object of which value has to be printed 您错过了必须打印其值的对象

for (var i in zk) {
    for (var j in zk[i]) {
        console.log(zk[i][j])
    }
}

The issue is that you're missing one level of your loop. 问题是您缺少循环的一级。 You have to loop over the outer array, then loop over the keys in each object, then if that key's value is an array loop over that array. 您必须遍历外部数组,然后遍历每个对象中的键,然后如果该键的值是该数组的数组遍历。

If for example you want to log all the values then you can use Object.prototype.keys() it returns an array of the keys in an object so you can try something like this: 例如,如果要记录所有值,则可以使用Object.prototype.keys()返回对象中键的数组,因此您可以尝试如下操作:

 const arr = [ {"key1": ["2019-04-12-14:54:29.190", "19", "0", "4325", "1"]}, {"key2": ["2019-04-12-14:54:29.191", "20", "0", "2212", "1"]}, {"key3": ["2019-04-12-14:54:29.192", "22", "0", "4376", "0"]} ]; arr.forEach(x => { Object.keys(x).forEach(k => { if (Array.isArray(x[k])) { x[k].forEach(v => { console.log(v); }); } else { console.log(x[k]); } }); }); 

Object.entries would be helpful if you are trying to iterate over and output each object key and each of the values in the corresponding array. 如果您尝试迭代并输出每个对象键和对应数组中的每个值,则Object.entries会有所帮助。 There are more compact ways to do this, but the nested loops should clearly illustrate what is happening. 有更紧凑的方法可以执行此操作,但是嵌套循环应该清楚地说明正在发生的事情。

 const arr = [ {"key1": ["2019-04-12-14:54:29.190", "19", "0", "4325", "1"]}, {"key2": ["2019-04-12-14:54:29.191", "20", "0", "2212", "1"]}, {"key3": ["2019-04-12-14:54:29.192", "22", "0", "4376", "0"]} ]; for (let obj of arr) { for (let [key, values] of Object.entries(obj)) { console.log(`KEY: ${key}`); console.log('VALUES: '); for (let value of values) { console.log(value); } } } // Example output from first object... // KEY: key1 // VALUES: // 2019-04-12-14:54:29.190 // 19 // 0 // 4325 // 1 

In js you always get the keys so what you do is: 在js中,您总是会得到密钥,因此您要做的是:

for (var i in zk) {
    for (var j in zk[i]) { // j wil ben key1, key2 ect
        console.log(zk[i][j]); // arrays  ["2019-04-12-14:54:29.190", "19", "0", "4325", "1"] over where you can itterate
        for (var k in zk[i][j]) {
            console.log(zk[i][j][k]); // "2019-04-12-14:54:29.190" ect
        }
    }
}

Use Object.values : 使用Object.values

 const arr = [ {"key1": ["2019-04-12-14:54:29.190", "19", "0", "4325", "1"]}, {"key2": ["2019-04-12-14:54:29.191", "20", "0", "2212", "1"]}, {"key3": ["2019-04-12-14:54:29.192", "22", "0", "4376", "0"]} ]; arr.forEach(obj => console.log(Object.values(obj))); 
 .as-console-wrapper { max-height: 100% !important; top: auto; } 

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

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