Filter array of objects with another array of object

被刻印的时光 ゝ 提交于 2019-12-03 17:09:52
      var filtered = [];

      for(var arr in myArray){
         for(var filter in myFilter){
             if(myArray[arr].userid == myFilter[filter].userid && myArray[arr].projectid == myFilter[filter].projectid){
                filtered.push(myArray[arr].userid);
               }
         }
      }
      console.log(filtered);

This may help you

Thanks in Advance

myArray = [
  {
    userid: "100",
    projectid: "10",
    rowid: "0"
  },
  {
    userid: "101",
    projectid: "11",
    rowid: "1"
  },
  {
    userid: "102",
    projectid: "11",
    rowid: "2"
  },
  {
    userid: "102",
    projectid: "13",
    rowid: "3"
  },
  {
    userid: "101",
    projectid: "10",
    rowid: "4"
  }
];

myFilter = [
  {
    userid: [
      {
        0: "101"
      },
      {
        1: "102"
      }
    ],
    projectid: [
      {
        0: "11"
      }
    ]
  }
];

const filterFn = (array, filter) => {
  let result = [];
  filter.forEach(element => {
    let keys = Object.keys(element);
    keys.forEach(key => {
      let values = Object.values(element[key]);
      values = values.map(x => Object.values(x)[0]);
      let ans = array.filter(e => {
        if (values.includes(e[key])) return e;
      });
      result = result.concat(ans);
    });
  });
  return result;
};
console.log(filterFn(myArray, myFilter));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!