0

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!

1
  • This containsObject method should work. There is probably a problem with the calling code. Could you show it (make an autonomous reduced code displaying the problem if possible) ? Commented Mar 14, 2012 at 13:36

3 Answers 3

3

You need to change the equality test to compare the keys:

function containsObject(obj, list) {
    var i;
    for (i = 0; i < list.length; i++) {
        if (list[i].key === obj.key) {
            return true;
        }
    }

    return false;
}
Sign up to request clarification or add additional context in comments.

1 Comment

That worked perfectly. Shows how little I know about javascript. I've only just start learning it. So thanks a lot! much appreciated!
1

I have got this function that gets added to the prototype-chain of the Array-Object, so you can just call list.hasObject(obj).

Array.prototype.hasObject = (
  !Array.indexOf ? function (o)
  {
    var l = this.length + 1;
    while (l -= 1)
    {
        if (this[l - 1] === o)
        {
            return true;
        }
    }
    return false;
  } : function (o)
  {
    return (this.indexOf(o) !== -1);
  }
);

small fiddle for this: http://jsfiddle.net/NE9kx/

1 Comment

Good answer, but I have gone with the above one. Thanks anyway!
1

Maybe it's not the answer you're looking for, but I wonder why don't use an Object instead of an Array, if you have a key:

var objectList = {};

var myObject { key : '1234', label : 'Richard Clifford' };

objectList[myObject.key] = myObject;

So if you want to iterate:

for (var key in objectList) {
    if (objectList.hasOwnProperty(key)
        alert(key);
}

If you want to access to the object with a key given you have just to:

alert(objectList['1234'].label);

1 Comment

The reason why is due to the third-party software which I am writing an extension for, it uses an array of objects. Thanks for the suggestion for future reference I will use objects :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.