Answers for "filter from two arrays of objects javascript"

2

filter using two array of objects

const arr1 = [{id:'1',name:'A'},{id:'2',name:'B'},{id:'3',name:'C'},{id:'4',name:'D'}];
const arr2 = [{id:'1',name:'A',state:'healthy'},{id:'3',name:'C',state:'healthy'}];
const filterByReference = (arr1, arr2) => {
   let res = [];
   res = arr1.filter(el => {
      return !arr2.find(element => {
         return element.id === el.id;
      });
   });
   return res;
}
console.log(filterByReference(arr1, arr2));
Posted by: Guest on May-24-2021
1

compare and filter two array of object

array1 = [{path: "hello"}, {path: "world"}];
array2 = [{path: "hello"}, {path: "hahah", text: "dsdsds"}];

//comparing array1 and array2 and returning unmatced from array1

Object.keys(array1)
  .filter((val, index) => array1[index].path !== array2[index].path)
  .map((val, index) => array1[index]);

// output
//[{"path": "world"}]
Posted by: Guest on September-23-2021

Code answers related to "filter from two arrays of objects javascript"

Browse Popular Code Answers by Language