如何按字段名称列出 JavaScript 对象的属性
How to list the properties of a JavaScript object by field name
我创建了这样一个对象:
[
Object {
Username = "James", Password = "12345", Email = "james@test.com"
},
Object {
Username = "Auric", Password = "12345", Email = "auric@test.com"
}
]
检索 属性 姓名列表的最佳方法是什么?即我想以一些变量 'keys' 结束,这样:
Object = ["james@test.com", "auric@test.com"]
谢谢。
您可以使用.map
var data = data.map(function (el) {
return el.Email
})
你有一个对象数组,所以你需要遍历它然后 return 你需要的值。
var myObjects = [
{
"Username" : "James",
"Password" : "12345",
"Email" : "james@test.com"
},
{
"Username" : "Auric",
"Password" : "12345",
"Email" : "auric@test.com"
}
];
function getProps (key) {
var values = [];
myObjects.forEach(function (obj){
values.push(obj[key]);
});
return values;
}
console.log(getProps('Email'));
我创建了这样一个对象:
[
Object {
Username = "James", Password = "12345", Email = "james@test.com"
},
Object {
Username = "Auric", Password = "12345", Email = "auric@test.com"
}
]
检索 属性 姓名列表的最佳方法是什么?即我想以一些变量 'keys' 结束,这样:
Object = ["james@test.com", "auric@test.com"]
谢谢。
您可以使用.map
var data = data.map(function (el) {
return el.Email
})
你有一个对象数组,所以你需要遍历它然后 return 你需要的值。
var myObjects = [
{
"Username" : "James",
"Password" : "12345",
"Email" : "james@test.com"
},
{
"Username" : "Auric",
"Password" : "12345",
"Email" : "auric@test.com"
}
];
function getProps (key) {
var values = [];
myObjects.forEach(function (obj){
values.push(obj[key]);
});
return values;
}
console.log(getProps('Email'));