我有一个JSON对象数组
array = [{name: "will", age: "18"}
{name: "Elliott", age: "21"}
]
有没有办法让#34;年龄"对于任何给定的"名称" (就像在查询数据库时使用SQL语句一样?
答案 0 :(得分:2)
是
let array = [
{ name: "will", age: "18"},
{ name: "john", age: "18"},
{ name: "elliott", age: "21"}
]
array.filter((e) => { return e.name === 'john' })
结果
[ { name: 'john', age: '18' } ]
答案 1 :(得分:0)
let queryName = 'will';
array.forEach((obj) => {
if (obj.name === queryName) {
console.log(obj.name, obj.age); // will 18
}
});