如何从数组JavaScript中选择键并返回数组结果

时间:2018-11-01 02:38:49

标签: javascript

如何在JavaScript数组中选择数据作为PHP?

var myarray = [
            {name:"Nita",id:"001"},
            {name:"Lyda",id:"002"},
            {name:"Lika",id:"003"}
          ];
select(name,id).from(myarray).where(name like %L%);

如何选择这样的数据?

2 个答案:

答案 0 :(得分:0)

也许您可以结合使用filter(带有字符串match)和map

var myarray = [{name:"Nita",id:"001",age:52},
               {name:"Lyda",id:"002",age:34},
               {name:"Lika",id:"003",age:20}];

// select(name,id).from(myarray).where(name like %L%);
var res = myarray
          .filter(d => d.name.match('L'))
          .map(({name, id}) => ({name, id}));
       
console.log(res);

答案 1 :(得分:0)

用户filter为“ where”,map为“ select”。编写函数以实现所需的功能。例如,要仅选择id包含name的{​​{1}},可以编写:

L