1

I have an empty array: "testList = []" I want to have a function that adds objects only if it exists:

addIfNotInList({'keya':'a','keyb':'b'}, testList);
addIfNotInList({'keya1':'a5','keyb':'b2'}, testList);
addIfNotInList({'keya':'a','keyb':'b'}, testList);

The result of this should be:

testList = [{'keya':'a','keyb':'b'},{'keya1':'a5','keyb':'b2'}]

Usually if it were not an object, I would just do: if(testList.indexOf(stringvalue)) {testList.push(stringvalue)}

Though I've discovered this does not work with objects.

3
  • 1
    And you have tried... what? Commented Jan 26, 2016 at 4:20
  • Possible duplicate of Javascript array.indexOf doesn't search objects Commented Jan 26, 2016 at 4:24
  • 1
    What does addIfNotInList() mean? Does it mean there's no object with all the same keys already in the array? Or no matching keya value? Commented Jan 26, 2016 at 4:26

4 Answers 4

1
JSON.stringify(obj1) === JSON.stringify(obj2);

You can compare the objects with the above code but there's alittle trick in this.It results in true if both objects are like {x: 100, y: 1} and {x: 100, y: 1} but shows false if {x: 100, y: 1} and {y: 1, x: 100}.

Sign up to request clarification or add additional context in comments.

2 Comments

JSON.stringify({a:1, b:2}) == JSON.stringify({b:2, a:1}) ... false
Yes I just wrote that it would result in false if you change the order
1

You can use a comparator

    var testList = [];

    var myComparator = function (obj1, obj2) {
      // check if they are the same
      return obj1.keya === obj2.keya && obj1.keyb === obj2.keyb;  
    };

    var addIfNotInList = function(obj, list, comparator) {
        // you can also declare the comparing function here as default
        comparator = comparator || myComparator;

        // check if already in the list
        var exists = list.some(function(listItem) {
            return comparator(obj, listItem);
        });

        if(!exists) {
            list.push(obj);
        }
    };

    addIfNotInList({'keya':'a','keyb':'b'}, testList);
    addIfNotInList({'keya':'a5','keyb':'b2'}, testList);
    addIfNotInList({'keya':'a','keyb':'b'}, testList);

    console.log(testList);

You can also use underscore.js's utility functions for comparison: isEqual

Comments

0

I would use something like this, which loops through the array and checks each item against your argument. Each time it doesn't match counter increases, so if counter is as large as the array's length at the end, then the object must not match any that is already in there. Hope this helps!

var addIfNotInList = function(object, testList){

var counter = 0;

    for(var i = 0; i < testList.length, i++){

        if(testList[i]) !== object){

        counter++;

        }

    }

    if(counter === testList.length){

    testList.push(object)

    }

};

Comments

0

You should compare object equality by looping all elements in testList. if you use underscorejs, it is pretty easy.

function addIfNotInList(obj, list) {
    var doesNotExist = _.every(list, function(item) {
        return !_.isEqual(item, obj);
    });
    if (doesNotExist) {
        list.push(obj);
    }
}

Comments

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.