最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Compare two javascript arrays and remove duplicate entries - Stack Overflow

programmeradmin0浏览0评论

I am trying to pare 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 pare 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.

I am trying to pare 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 pare 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.

Share Improve this question edited Mar 23, 2019 at 6:08 T3rr11 asked Mar 22, 2019 at 4:48 T3rr11T3rr11 579 bronze badges 7
  • You need to write your own pare method. – Shubham Commented Mar 22, 2019 at 4:52
  • I think your question is lacking some information. What is the desired output? What do you want it to look like. Like, do you want to know what's new or remove duplicates? – Sinaesthetic Commented Mar 22, 2019 at 4:52
  • Like @Shubham said, you're going to need to pare each key value pair in each item, then push the items that are unique to a new array – Andria Commented Mar 22, 2019 at 4:52
  • I've updated the post @Sinaesthetic also fair enough, How would i pare each key value, Would that be through a for loop for each item? My main array has 270 items in it. So would that means it needs to scan all 230 items for 231 times if there is a new item? That seems very load intensive. – T3rr11 Commented Mar 22, 2019 at 4:57
  • 1 There's no such thing as a JSON array. JSON is always a string. You have an array of objects. – Mulan Commented Mar 22, 2019 at 5:02
 |  Show 2 more ments

4 Answers 4

Reset to default 5

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);

If you know your properties will always be in the same order you can use JSON.stringify to serialize the objects and pare 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);

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 pare = (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(pare(array1, array2));

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

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)

发布评论

评论列表(0)

  1. 暂无评论