简体   繁体   English

如何一一提取作为关联数组(对象)值的数组元素

[英]How to extract array elements that are values ​of an associative array (object) one by one

I'm trying to extract one array element at a time from an associative array (object) using JavaScript.我正在尝试使用 JavaScript 从关联数组(对象)中一次提取一个数组元素。 I tried the method of extracting values ​​from an associative array, but the result was not as expected.我尝试了从关联数组中提取值的方法,但结果并不如预期。

let obj = {
  label1: [1.1, 1.4],
  label2: [1.2, 1.5],
  label3: [1.3, 1.6],
}

function test() {
  Object.keys(obj).forEach(key => {
    obj[key].forEach(elm => {
        console.log(elm[0]) //1
        console.log(elm[1]) //.
    }
  }
}

I was expecting the following results, but 1.1 was broken down.我期待以下结果,但 1.1 被分解了。

console.log(elm[0]) //1.1
console.log(elm[1]) //1.4
console.log(elm[2]) //1.7

Is there an effective way to extract this array element successfully?有没有一种有效的方法可以成功提取这个数组元素?

You don't need access subproperties of your "elm"您不需要访问“榆树”的子属性

Just use只需使用

function test(obj) {
    Object.keys(obj).forEach(key => {
        //obj[key] is [1.1, 1.4]
        obj[key].forEach(elm => {
            //elm is 1.1
            console.log(elm);
        });
    });
}

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

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