我有一个包含多个对象的对象,并且需要为每个对象中的每个键创建一个循环。
我需要控制台来记录这样的内容。
Iron
TIBC
carbon dioxide
Sodium
etc...
这是我迄今为止尝试过的
(function() {
var blood = {
"blood": {
"one": {
"iron": "Iron",
"tibc": "TIBC",
},
"two": {
"cd": "Carbon Dioxide",
"sodium": "Sodium",
"potassium": "Potassium",
"chloride": "Chloride",
},
"three": {
"cholesterol": "Cholesterol",
"dhdl": "Direct HDL",
},
"four": {
"rigg": "Rubella IgG",
"hbsag": "HBsAg",
"ahiv": "Anti-HIV 1+2",
"estrdiol": "Estradiol",
"fsh": "FSH",
}
}
}
for (var key in blood) {
if (blood.hasOwnProperty(key)) {
var newKey = blood[key]
for (var key in newKey) {
if (newKey.hasOwnProperty(key)) {
//do stuff
console.log(newKey[key])
}
}
}
}
})
最佳答案
这就是 Object.values 的用途:
const result = Object.values( blood.blood ).map(
obj => Object.values(obj).join("\n")
).join("\n");
console.log( result );
关于javascript - 循环遍历多维对象内的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46223828/