Home > Back-end >  How to compare two array and remove all items which are in the first array? JS
How to compare two array and remove all items which are in the first array? JS

Time:01-31

I have two arays. example of arrays:

let firstArr = 
[{id: 1, name: "test 1" , title : "test 11"} ,
{id: 2, name: "test 2" , title : "test 22"} ,
{id: 3, name: "test 3" , title : "test 3"}]

let secondArr = 
[{id: 1, name: "test 1" , title : "test 11"} ,
{id: 2, name: "test 2" , title : "test 22"} ,
{id: 3, name: "test 3" , title : "test 3"} ,
{id: 4, name: "test 4" , title : "test 4}]

the difference between these two arrays is only the object located in the second array with id 4

this:

 {id: 4, name: "test 4" , title : "test 4}

What I need ?

I need to loop thought both array or whatever and remove all items which are in the first row.

After loop my neew array need to look like :

newArr = [{id: 4, name: "test 4" , title : "test 4}];

I will not write to you what I was trying to do because I will only confuse you.

EDIT: my try ->

         let concatArray = [...firstArray, ...secondArray];
         let uniqueItems = [...new Set(concatArray)];
         uniqueItems.filter((arr) => firstArray.id !== col.id);

This is no work...

merging two arrays I get nothing. I need to delete all items from the first array just.

CodePudding user response:

You can use some:

const firstArr =
  [{id: 1, name: 'test 1' , title : 'test 11'},
  {id: 2, name: 'test 2' , title : 'test 22'},
  {id: 3, name: 'test 3' , title : 'test 3'}];

const secondArr = [{id: 1, name: 'test 1', title : 'test 11'} ,
  {id: 2, name: 'test 2' , title : 'test 22'} ,
  {id: 3, name: 'test 3' , title : 'test 3'} ,
  {id: 4, name: 'test 4' , title : 'test 4'}];

let newArray = secondArr.filter(item => !firstArr.some(item2 => item2.id === item.id));

In spoken words: In newArray, look into secondArr and get me a list of items where they are not found in anywhere in firstArr.

CodePudding user response:

We can use the Array filter function, and the findIndex function which allows us to find the index of an item in the array that meets a certain condition. If the item is not present, the index will be -1, and that's how we can identify the missing objects.

const filtered = secondArr.filter((secondItem) => {
    return firstArr.findIndex(({ id: firstItemId }) => firstItemId === secondItem.id) === -1;
}) 

console.log(filtered); // [ { id: 4, name: 'test 4', title: 'test 4' } ]

CodePudding user response:

Lodash if you don't mind.

const firstArr =[{id: 1, name: 'test 1' , title : 'test 11'},{id: 2, name: 'test 2' , title : 'test 22'},{id: 3, name: 'test 3' , title : 'test 3'}];
const secondArr = [{id: 1, name: 'test 1', title : 'test 11'} ,{id: 2, name: 'test 2' , title : 'test 22'} ,{id: 3, name: 'test 3' , title : 'test 3'} ,{id: 4, name: 'test 4' , title : 'test 4'}];

const notInBoth = [
    ..._.differenceWith(firstArr, secondArr, _.isEqual),
    ..._.differenceWith(secondArr, firstArr, _.isEqual),
  ];
  
console.log(notInBoth);
.as-console-wrapper{min-height: 100%!important; top: 0}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

CodePudding user response:

array1 = array1.filter(function(val) {
  return array2.indexOf(val) == -1;
});

Or, with the availability of ES6:

array1 = array1.filter(val => !array2.includes(val));
  •  Tags:  
  • Related