如何枚举下面的对象?我事先不知道密钥,可能会更多
{ '10.193.105.1':
{ location: '02123',
hostname: 'A0C2',
contact: 'root@localhost' },
'10.199.107.1':
{ location: '01164',
hostname: 'cp11',
contact: 'me' } }
答案 0 :(得分:3)
Object.keys为您提供对象键。嵌套和循环
var obj = {
'10.193.105.1': {
location: '02123',
hostname: 'A0C2',
contact: 'root@localhost'
},
'10.199.107.1': {
location: '01164',
hostname: 'cp11',
contact: 'me'
}
};
Object.keys(obj).forEach( function(key) { //level one
var child = obj[key];
console.group(key);
Object.keys(child).forEach( function (prop) { //level two
console.log(prop, ":", child[prop]);
});
console.groupEnd(key);
} );