js根据条件筛选出合适的数组

    let arr = [
      { name: "zs", age: 18, id: 1, sex: 0 },
      { name: "ls", age: 19, id: 2, sex: 0 },
      { name: "zs", age: 19, id: 3, sex: 1 }
    ];

找到对象group所对应的数据,返回一个新数组

let group = { name: "zs", age: 19 };
    let newList = arr.filter((item, index) => {
      let flag = true;
      for (let key in group) {
        if (group[key] != "") {
          if (group[key] != item[key]) {
            flag = false;
          }
        }
      }
      if (flag) {
        return item;
      }
    });
    console.log(newList);

 

posted @ 2022-08-09 17:44  黑白棋学弟  阅读(450)  评论(0编辑  收藏  举报