T3rr11
T3rr11

Reputation: 57

Compare two javascript arrays and remove duplicate entries

I am trying to compare two different arrays together, One previous and one current. The previous set of data contains:

[
  {"member_name":"Test1","item":"Sword"},
  {"member_name":"Test2","item":"Sword"}
]

The new set contains:

[
  {"member_name":"Test1","item":"Sword"},
  {"member_name":"Test2","item":"Sword"},
  {"member_name":"Test1","item":"Shield"}
]

So as you can see here Test1 has obtained a new item. I've tried to compare these two arrays together a few ways but with no avail.

The methods I've tried:

This one just returns the entire array. Not individual items.

Items_Curr.filter(function(item) { return !Items_Prev.includes(item); });

This method just returns 3 Undefined's.

Items_Curr.map(e => { e.member_name });

I've been looking through trying to find a way of doing this but other posts just explain methods to determine change in simpler arrays.

E.G [a,b] - [a, b, c]

Update:

The end goal is I'd like to create a new array 'NewItems' which would contain an array of all the newly added names and items. So if there is change, I would like that to be broadcasted, if there is no change, then ignore until the function has been ran again.

Upvotes: 3

Views: 1677

Answers (4)

Nick Parsons
Nick Parsons

Reputation: 50749

Realistically you want to do something like:

[a, b, c] - [a, b] 

Which would give you c. You can achieve this using .some, which allows you to "customize" the includes functionality.

See example below:

const arr1 = [
  {"member_name":"Test1","item":"Sword"},
  {"member_name":"Test2","item":"Sword"}
];

const arr2 = [
  {"member_name":"Test1","item":"Sword"},
  {"member_name":"Test2","item":"Sword"},
  {"member_name":"Test1","item":"Shield"}
];

const res = arr2.filter(({member_name:a, item:x}) => !arr1.some(({member_name:b, item:y}) => a === b && x === y));
console.log(res);

Upvotes: 5

Serene Abraham Mathew
Serene Abraham Mathew

Reputation: 388

You can acheieve it using array methods filter() and findIndex()

Filter current array with out put of findIndex() function on previous array

var prevItems = [
  {"member_name":"Test1","item":"Sword"},
  {"member_name":"Test2","item":"Sword"}
]

var currItems = [
  {"member_name":"Test1","item":"Sword"},
  {"member_name":"Test2","item":"Sword"},
  {"member_name":"Test1","item":"Shield"},
  {"member_name":"Test2","item":"Shield"}
]

var newItems = currItems.filter(function(currItem ){
  return  prevItems.findIndex(function(prevItem){
     return prevItem.member_name ==  currItem.member_name &&
            prevItem.item == currItem.item
  }) == -1
})


console.log(newItems)

Upvotes: 0

Shubham
Shubham

Reputation: 1793

I think something like this you need to do if keys of objects are not changing and new items are only added at last.

const array1 = [
  {"member_name":"Test1","item":"Sword"},
  {"member_name":"Test2","item":"Sword"}
];

const array2 = [
  {"member_name":"Test1","item":"Sword"},
  {"member_name":"Test2","item":"Sword"},
  {"member_name":"Test1","item":"Shield"}
];

const compare = (array1, array2) => {
	if (array1.length !== array2.length) {
  	return false;
  }
  
  for (let i = 0; i < array1.length; i += 1) {
    if (array1[i].member_name !== array2[i].member_name) {
    	return false;
    }
    
    if (array1[i].item !== array2[i].item) {
    	return false;
    }
  }
  
  return true;
};

console.log(compare(array1, array2));

If order of objects are changing, then you need to write sorting algo for array and then compare.

Upvotes: 1

Brian Adams
Brian Adams

Reputation: 45810

If you know your properties will always be in the same order you can use JSON.stringify to serialize the objects and compare the results:

const Items_Prev = [
  {"member_name":"Test1","item":"Sword"},
  {"member_name":"Test2","item":"Sword"}
]

const Items_Curr = [
  {"member_name":"Test1","item":"Sword"},
  {"member_name":"Test2","item":"Sword"},
  {"member_name":"Test1","item":"Shield"}
]

const serialized_Items_Prev = Items_Prev.map(i => JSON.stringify(i));
const NewItems = Items_Curr.filter(i => !serialized_Items_Prev.includes(JSON.stringify(i)));
console.log(NewItems);

Upvotes: 2

Related Questions