如何遍历 Javascript 对象的属性名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1876485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to iterate through property names of Javascript object?
提问by eiu165
I would like to get the property names from a Javascript object to build a table dynamically. Example:
我想从 Javascript 对象中获取属性名称以动态构建表。例子:
var obj = {'fname': 'joe', 'lname': 'smith', 'number': '34'};
for (var i = 0; i < obj.properties.length; i++) {
alert(' name=' + obj.properties[i].name + ' value=' + obj.properties[i].value);
}
would alert:
会提醒:
name=fname value=joe
name=lname value=smith
name=number value=34
Then I could build a table using object like this:
然后我可以使用这样的对象构建一个表:
var obj = { 'players': [
{ 'fname': 'joe', 'lname': 'smith', 'number': '34'} ,
{ 'fname': 'jim', 'lname': 'Hoff', 'number': '12'} ,
{ 'fname': 'Hyman', 'lname': 'jones', 'number': '84'}
] };
to produce:
生产:
| fname | lname | number |
|-------|--------|---------|
| joe | smith | 34 |
| jim | Hoff | 12 |
| Hyman | jones | 84 |
UPDATE
更新
Thanks to the answer, I have produced a table from the Javascript objects using the property names from the first object in the list for the headers:
感谢答案,我使用列表中第一个对象的属性名称从 Javascript 对象生成了一个表作为标题:
function renderData() {
var obj = { 'players': [
{ 'fname': 'joe', 'lname': 'smith', 'number': '34' },
{ 'fname': 'jim', 'lname': 'jones', 'number': '12' },
{ 'fname': 'Hyman', 'lname': 'Hoff', 'number': '84' }
] };
var cols = GetHeaders(obj);
$('#Output').html(CreateTable(obj, cols));
}
function CreateTable(obj, cols) {
var table = $('<table></table>');
var th = $('<tr></tr>');
for (var i = 0; i < cols.length; i++) {
th.append('<th>' + cols[i] + '</th>');
}
table.append(th);
for (var j = 0; j < obj.players.length; j++) {
var player = obj.players[j];
var tr = $('<tr></tr>');
for (var k = 0; k < cols.length; k++) {
var columnName = cols[k];
tr.append('<td>' + player[columnName] + '</td>');
}
table.append(tr);
}
return table;
}
function GetHeaders(obj) {
var cols = new Array();
var p = obj.players[0];
for (var key in p) {
//alert(' name=' + key + ' value=' + p[key]);
cols.push(key);
}
return cols;
}
回答by jldupont
Use for...inloop:
使用for...in循环:
for (var key in obj) {
console.log(' name=' + key + ' value=' + obj[key]);
// do some more stuff with obj[key]
}
回答by rab
In JavaScript 1.8.5, Object.getOwnPropertyNamesreturns an array of all properties found directly upon a given object.
在 JavaScript 1.8.5 中,Object.getOwnPropertyNames返回直接在给定对象上找到的所有属性的数组。
Object.getOwnPropertyNames ( obj )
and another method Object.keys, which returns an array containing the names of all of the given object's ownenumerable properties.
和另一个方法Object.keys,它返回一个数组,其中包含所有给定对象自己的可枚举属性的名称。
Object.keys( obj )
I used forEachto list values and keys in obj, same as for (var key in obj) ..
我曾经forEach在obj中列出值和键,与for (var key in obj) ..
Object.keys(obj).forEach(function (key) {
console.log( key , obj[key] );
});
This all are new features in ECMAScript , the mothods getOwnPropertyNames, keyswon't supports old browser's.
这一切都是在ECMAScript中的新功能,该mothods getOwnPropertyNames,keys不会支持旧的浏览器。

