Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter array of objects with another array of objects

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 an array of objects like this:

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" } ...] 

I want to filter it with an array like this:

myFilter = [ {     userid: "101",      projectid: "11" }, {     userid: "102",      projectid: "12" }, {     userid: "103",      projectid: "11" }] 

and return this (the userid and the projectid in myFilter need to match the userid and the projectid in myArray):

myArrayFiltered = [ {     userid: "101",      projectid: "11",     rowid: "1" }, {     userid: "102",      projectid: "12",     rowid: "2" }] 

How can I do that ?

like image 827
GtAntoine Avatar asked Jun 23 '15 14:06

GtAntoine


People also ask

How do I filter an array from another array?

const arr1 = [4, 23, 7, 6, 3, 6, 4, 3, 56, 4]; const arr2 = [4, 56, 23]; We are required to write a JavaScript function that takes in these two arrays and filters the first to contain only those elements that are not present in the second array.

How do you filter two arrays of objects?

We are required to write a JavaScript function that takes in two such arrays. Our function should return a new filtered version of the first array (arr1 in this case) that contains only those objects with a name property that are not contained in the second array (arr2 in this case) with the same name property.


Video Answer


2 Answers

With Ecma script 6.

const myArrayFiltered = myArray.filter( el => {   return myfilter.some( f => {     return f.userid === el.userid && f.projectid === el.projectid;   }); }); 

Function:

const filterObjectArray = (arr, filterArr) => (     arr.filter( el =>         filterArr.some( f =>             f.userid === el.userid && f.projectid === el.projectid         )     ) );  console.log(filterObjectArray(myArray, myFilter)) 

Link to example

like image 23
Tabares Avatar answered Oct 11 '22 21:10

Tabares


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);
like image 82
Andy Avatar answered Oct 11 '22 22:10

Andy