I got an array of objects with structure like this
myArr = [
{name: Test1, age: 20, info: Test2},
{name: Test2, age: 20, info: Test3},
{name: Test5, age: 28, info: Test30}
]
With my custom action I am adding new info to array. But before adding new obejct into array I want to check if similiar one exist (same values for all keys). If yes - do nothing, if no similar object - add to array.
Simple for loop duplicates objects since it is running over all elements of the array.
var i;
for(i = 0; i < myArr.length; i++) {
if((myArr[i].name === added.name) && (myArr[i].age === added.age) && (myArr[i].info === added.info) {
}
else {
myArr.push(added);
}
}
var keys = Object.keys(added); if (!myArr.some(e => keys.every(k => added[k] === e[k]))) { /* ... */ }Throw inObject.keys(e).length === keys.lengthif you want that check (or tweak in about 18 other ways).