2

Im doing some stuff in NodeJS and need to compare Objects in different arrays for duplicate properties.

Example:

Items = [{"id": "10", "name": "milk"},{"id":"7", "name": "banana"},{"id":"47", "name": "cheese"},{"id":"6", "name":"carrot"}]
Fridge = [{"id":"19", "name": "cheese"},{"id": "10", "name": "milk"},{"id":"43","name": "egg"}, {"id":"6", "name":"not a carrot"}]

I want to get all the Objects in the array that have properties that are Included in both arrays. (Ugh bad explanation, see Example)

In this example, i want to get

Output = [{"id": "10", "name": "milk"},{"id":"6", "name":"not a carrot"}]

because only these 2 IDs exist in both Input Arrays.

How would i do this?

3 Answers 3

2

You can use includes method in combination with filter.

Items = [{"id": "10", "name": "milk"},{"id":"7", "name": "banana"},{"id":"47", "name": "cheese"},{"id":"6", "name":"carrot"}]
Fridge = [{"id":"19", "name": "cheese"},{"id": "10", "name": "milk"},{"id":"43","name": "egg"}, {"id":"6", "name":"not a carrot"}]

result = Fridge.filter(item => Items.map(f => f.id).includes(item.id));
console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

1

You can easily achieve this using lodash.js as below:

In case you want to check for id only:

    let Items = [{"id": "10", "name": "milk"},{"id":"7", "name": "banana"},{"id":"47", "name": "cheese"},{"id":"6", "name":"carrot"}]
let Fridge = [{"id":"19", "name": "cheese"},{"id": "10", "name": "milk"},{"id":"43","name": "egg"}, {"id":"6", "name":"not a carrot"}]

let result = _.intersectionBy(Fridge, Items, 'id');

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

Or if you want to compare entire object:

let Items = [{"id": "10", "name": "milk"},{"id":"7", "name": "banana"},{"id":"47", "name": "cheese"},{"id":"6", "name":"carrot"}]
let Fridge = [{"id":"19", "name": "cheese"},{"id": "10", "name": "milk"},{"id":"43","name": "egg"}, {"id":"6", "name":"not a carrot"}]

let result = _.intersectionWith(Items, Fridge, _.isEqual);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

`

Comments

0

Just another approach that could be useful to find duplicates with n arrays of objects as input:

var Items = [{"id": "10", "name": "milk"},{"id":"7", "name": "banana"},{"id":"47", "name": "cheese"},{"id":"6", "name":"carrot"}];
var Fridge = [{"id":"19", "name": "cheese"},{"id": "10", "name": "milk"},{"id":"43","name": "egg"}, {"id":"6", "name":"not a carrot"}];

var mergedArrray = Items.concat(Fridge); //merge all
//array1 = array1.concat(array2, array3, array4, ..., arrayN);    


var duplicateIds = [];
var uniqueIds = [];

//The idea is look for the id in uniqueIds, if exist push into duplicateIds
//if not push into uniqueIds 
mergedArrray.map((obj,v)=>{
(uniqueIds.indexOf(obj.id) == -1)?uniqueIds.push(obj.id):duplicateIds.push(obj.id);
})

console.log(duplicateIds);
console.log(uniqueIds);

This solution doesn't return an array of duplicates, just the reference.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.