查询typescript中的对象数组

时间:2017-03-30 11:40:25

标签: arrays json ionic2

我有一个JSON对象数组

array = [{name: "will", age: "18"}
         {name: "Elliott", age: "21"}
        ] 

有没有办法让#34;年龄"对于任何给定的"名称" (就像在查询数据库时使用SQL语句一样?

2 个答案:

答案 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
    }
});