为了
var data = { "title1": {"subtitle1": "one", "subtitle2": "two"}, "title2": "three", "title3": "four" };
我可以使用:
for (var key in data) {
console.log(key);
}
得到:
title1
title2
title3
我可以使用:
var key = Object.keys(data)[0];
for (var prop in data[key]){
console.log(data[key][prop]);
}
得到:
one
two
问题:用什么可以得到:
one
two
three
four
最佳答案
试试这个
var data = {
"title1": {
"subtitle1": "one",
"subtitle2": "two"
},
"title2": "three",
"title3": "four"
};
var output = [];
Object.keys(data).forEach(function(key) {
if (typeof data[key] == "object") {
Object.keys(data[key]).forEach(function(innerKey) {
output.push(data[key][innerKey])
});
} else {
output.push(data[key]);
}
});
console.log(output);
关于javascript - 在不知道名称的情况下访问对象中对象的多个属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38246686/