我有一个JSON对象说:
{
[
{
'name':'personA',
'age':'30',
'description':'some-text'
},
{
'name':'personB',
'age':'40',
'description':'some-text'
}
]
}
这个问题是我需要能够按名称访问每个var,例如:
people.PersonA.age:
所以与其:
people =
{
'personA':{
'age':'30',
'description':'some-text'
},
'personB':{
'age':'40',
'description':'some-text'
}
}
但后来我无法生成姓名列表
最好的结构是什么?我需要能够遍历名称并使用名称作为键来快速访问年龄等
用这种列出所有名字的方式呢?
var people = {
'personA': {'age': '30', 'description': 'some-text'},
'personB': {'age': '40', 'description': 'some-text'}
};
Object.keys(people).forEach(function(name) {
console.log(name);
});
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句