I have tried many things to get this working correctly but I can't
Here is my problem:
I have an array of objects in javascript which looks something like this:
var myArrOfObjs = [];
var myObject1 = {};
myObject1.key = '1234';
myObject1.label = 'Richard Clifford';
myArrOfObjs.push( myObject1 );
and I need to do something like:
if( !containsObject( myObject1, myArrOfOjbs ) ){
// Do stuff
}
I need the containsObject
function to check for key values within the found object (if any), so if containsObject( myObject1, myArrOfOjbs )
finds the object, I need to check to see if the key of that is the same as the one I am currently trying to push.
The reason I need it to check the keys is because I have tried this function which I found else where on StackOverflow, but it isn't quite working.
function containsObject(obj, list) {
var i;
for (i = 0; i < list.length; i++) {
if (list[i] == obj) {
return true;
}
}
return false;
}
It still pushes the object to the array even when it already contains the object.
Please let me know if you need anything clearing up, I realise that it isn't the easiest post to read/understand.
Thanks!