Filtering Array of Objects with Another Array of Objects
Consider the following arrays of objects:
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" } ...]
myFilter:
[ { userid: "101", projectid: "11" }, { userid: "102", projectid: "12" }, { userid: "103", projectid: "11" }]
The goal is to filter myArray using myFilter such that only objects in myArray that have matching userid and projectid values with the objects in myFilter are included in the filtered array.
Solution:
We can employ the filter and some array methods to achieve this filtering:
<code class="javascript">const myArrayFiltered = myArray.filter((el) => { return myFilter.some((f) => { return f.userid === el.userid && f.projectid === el.projectid; }); });</code>
Explanation:
The above is the detailed content of How to Filter an Array of Objects Based on Matching Properties with Another Array of Objects?. For more information, please follow other related articles on the PHP Chinese website!