JSFiddle - React, Tailwind, and code Playground
JavaScript
var a = {
name: 'bob'
};
var b = {
name: 'nick'
};
var c = {
name: 'bob'
};
var array = [];
array.push(a);
array.push(b);
console.log('old:');
console.log(JSON.stringify(array));
if (checkIfObjectExists(array, c)) {
// objects exists, do nothing
}
else {
array.push(c);
}
console.log('array should be equal:');
console.log('new:');
console.log(JSON.stringify(array));
// 2nd check
console.log('old:');
console.log(JSON.stringify(array));
c.name = 'bob1';
if (checkIfObjectExists(array, c)) {
// objects exists, do nothing
}
else {
array.push(c);
}
console.log('array shouldn\'t be equal:');
console.log(JSON.stringify(array));
function checkIfObjectExists(array, newObject) {
var i = 0;
for(i = 0; i < array.length; i++ ) {
var object = array[i];
if(JSON.stringify(object) === JSON.stringify(newObject))
{
return true;
}
}
return false;
}