gpt4 book ai didi

javascript - 如何遍历嵌套对象并将值放入数组?

转载 作者:行者123 更新时间:2023-11-29 22:52:41 25 4
gpt4 key购买 nike

我创建了一个包含以下格式的嵌套对象:

var lineMap = {
2016: {
Hemoglobin: 33 ,
Lysozyme: 33 ,
Myoglobin: 47 ,
DNA: 13 ,
Cytochrome C: 33
},
2017: {
Hemoglobin: 8 ,
Lysozyme: 47 ,
Myoglobin: 8 ,
DNA: 12 ,
Cytochrome C: 33
},
2018: {
Hemoglobin: 8 ,
Lysozyme: 33 ,
Myoglobin: 47 ,
DNA: 12 ,
Cytochrome C: 13
},
2019: {
Hemoglobin: 8 ,
Lysozyme: 8 ,
Myoglobin: 47 ,
DNA: 8 ,
Cytochrome C: 47
}
}

我想将每年的项目计数放入其自己的数组索引中,这样:

var arr = [
[33, 33, 47, 13, 33],
[8, 47, 8, 12, 33],
[8, 33, 47, 12, 13],
[8, 8, 47, 8, 47]
]

我尝试创建一个嵌套的 for 循环来遍历嵌套对象 lineMap

for (var i = 0; i < year.length; i ++){
for (var j = 0; j < itemName.length; j++){
temp_arr[j] = lineMap[year[i]][itemName[j]];
}
console.log("Index: " + i + "\n" + temp_arr);
arr[i] = temp_arr;
}
console.log(arr);

在第 5 行 (console.log(temp_arr)),控制台打印出我所期望的——其各自迭代的项目计数数组:

'Index: 0
33,33,47,13,33'
'Index: 1
8,47,8,12,33'
'Index: 2
8,33,47,12,13'
'Index: 3
8,8,47,8,47'

但是,在第 8 行 (console.log(arr)),我没有收到预期的输出。相反,我得到:

var arr = [
[8, 8, 47, 8, 47],
[8, 8, 47, 8, 47],
[8, 8, 47, 8, 47],
[8, 8, 47, 8, 47]
]

最佳答案

首先,一个警告:这些对象中属性的顺序定义的,现在,从 ES2015 开始,但是依赖那个顺序通常不是一个好主意。此外,您似乎只使用了 ES5 级别的功能,这些功能没有定义顺序。

因此,不依赖于顺序的最佳方法是按照您希望它们在结果数组中的顺序给自己一个属性名称数组:

var keys = [
"Hemoglobin",
"Lysozyme",
"Myoglobin",
"DNA",
"Cytochrome C"
];

然后是这样的(见评论):

// Loop through the years sorted lexicographically, building the result array
var arr = Object.keys(lineMap).sort().map(function(mainKey) {
// Loop through the property names in the defined order, building
// each inner array
var entry = lineMap[mainKey];
return keys.map(function(key) {
return entry[key];
});
});

实例:

var keys = [
"Hemoglobin",
"Lysozyme",
"Myoglobin",
"DNA",
"Cytochrome C"
];
var lineMap = {
2016: {
Hemoglobin: 33 ,
Lysozyme: 33 ,
Myoglobin: 47 ,
DNA: 13 ,
"Cytochrome C": 33
},
2017: {
Hemoglobin: 8 ,
Lysozyme: 47 ,
Myoglobin: 8 ,
DNA: 12 ,
"Cytochrome C": 33
},
2018: {
Hemoglobin: 8 ,
Lysozyme: 33 ,
Myoglobin: 47 ,
DNA: 12 ,
"Cytochrome C": 13
},
2019: {
Hemoglobin: 8 ,
Lysozyme: 8 ,
Myoglobin: 47 ,
DNA: 8 ,
"Cytochrome C": 47
}
};

// Loop through the years sorted lexicographically, building the
// result array
var arr = Object.keys(lineMap).sort().map(function(mainKey) {
// Loop through the property names in the defined order, building
// each inner array
var entry = lineMap[mainKey];
return keys.map(function(key) {
return entry[key];
});
});

console.log(arr);

如果您希望年份按数字顺序排列,只需向 sort 添加一个回调:

.sort(function(a, b) { return a - b; })

在 ES2015+ 世界中处理它,我仍然会按照你想要的顺序保留单独的键列表,所以没有太多变化,只是变得更简洁:

const arr = Object.entries(lineMap)
.sort(([a], [b]) => a.localeCompare(b))
.map(([_, entry]) => keys.map(key => entry[key]));

实例:

const keys = [
"Hemoglobin",
"Lysozyme",
"Myoglobin",
"DNA",
"Cytochrome C"
];
const lineMap = {
2016: {
Hemoglobin: 33 ,
Lysozyme: 33 ,
Myoglobin: 47 ,
DNA: 13 ,
"Cytochrome C": 33
},
2017: {
Hemoglobin: 8 ,
Lysozyme: 47 ,
Myoglobin: 8 ,
DNA: 12 ,
"Cytochrome C": 33
},
2018: {
Hemoglobin: 8 ,
Lysozyme: 33 ,
Myoglobin: 47 ,
DNA: 12 ,
"Cytochrome C": 13
},
2019: {
Hemoglobin: 8 ,
Lysozyme: 8 ,
Myoglobin: 47 ,
DNA: 8 ,
"Cytochrome C": 47
}
};

const arr = Object.entries(lineMap)
.sort(([a], [b]) => a.localeCompare(b))
.map(([_, entry]) => keys.map(key => entry[key]));

console.log(arr);

这利用了 Object.entries , arrow functions , 和 destructuring assignment .

再一次,如果你想按数字顺序排列这些年份:

const arr = Object.entries(lineMap)
.sort(([a], [b]) => a - b)
.map(([_, entry]) => keys.map(key => entry[key]));

关于javascript - 如何遍历嵌套对象并将值放入数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57185965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com