I've exhausted multiple attempts to solve this issue, but unfortunately, I can't seem to get it right.
Here's the problem at hand:
In my JavaScript code, I have an array of objects structured like this:
var myArrOfObjs = [];
var myObject1 = {};
myObject1.key = '1234';
myObject1.label = 'Richard Clifford';
myArrOfObjs.push( myObject1 );
Now, what I'm trying to achieve is something along these lines:
if( !containsObject( myObject1, myArrOfOjbs ) ){
// Do stuff
}
The containsObject
function needs to verify the key values within the object it finds. So, when
containsObject( myObject1, myArrOfOjbs )
locates the object, it should check if the key matches the one we're currently attempting to push.
I require this key-check functionality because I experimented with a similar function that I discovered on StackOverflow, but it isn't functioning as expected.
function containsObject(obj, list) {
var i;
for (i = 0; i < list.length; i++) {
if (list[i] == obj) {
return true;
}
}
return false;
}
Even with this function in place, the object still gets added to the array even if it's already present.
If there's any confusion or ambiguity in my explanation, please don't hesitate to ask for clarification. I understand that my post may not be the easiest to comprehend.
Thank you!