Filter array of objects with another array of objects

前端 未结 9 1751
小蘑菇
小蘑菇 2020-11-27 05:18

This question is similar to this one Jquery filter array of object with loop but this time I need to do the filter with an array of objects.

Exemple:

I have

9条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 06:04

    You can put a couple of array methods to use here - filter and some. They're available in all recent browsers, and there are polyfills available for the older browsers.

    const myArray = [{ userid: "100", projectid: "10", rowid: "0" }, { userid: "101", projectid: "11", rowid: "1"}, { userid: "102", projectid: "12", rowid: "2" }, { userid: "103", projectid: "13", rowid: "3" }, { userid: "101", projectid: "10", rowid: "4" }];
    const myFilter = [{ userid: "101", projectid: "11" }, { userid: "102", projectid: "12" }, { userid: "103",  projectid: "11"}];
    
    const myArrayFiltered = myArray.filter((el) => {
      return myFilter.some((f) => {
        return f.userid === el.userid && f.projectid === el.projectid;
      });
    });
    
    console.log(myArrayFiltered);

提交回复
热议问题