Best to check for duplicate before a arr.push()?
Hi all,
With a simply value, checking for duplicates before pushing to the array is like this:
myArray.indexOf(value) === -1) {myArray.push(value);}
But I am a bit lost when it come to an array like this:
myArray.push({
id: data.id,
first_name: data.first_name,
last_name: data.last_name
});
Any idea please?
Thank you.
@user1980 do you need to check by id only or whole object?
By id:
if (!myArray.find(item => item.id === data.id)) { // search by id
myArray.push({
id: data.id,
first_name: data.first_name,
last_name: data.last_name
});
}
for entire object - compare all props or use something like https://lodash.com/docs/4.17.15#isEqual
Please or to participate in this conversation.