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.

Rolando
  • 58,640
  • 98
  • 266
  • 407
  • 1
    And you have tried... what? – Sam Axe Jan 26 '16 at 04:20
  • Possible duplicate of [Javascript array.indexOf doesn't search objects](http://stackoverflow.com/questions/12604062/javascript-array-indexof-doesnt-search-objects) – Elezar Jan 26 '16 at 04: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? – jfriend00 Jan 26 '16 at 04:26

4 Answers4

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

Murat Ozgul
  • 11,193
  • 6
  • 29
  • 32
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}.

amanpurohit
  • 1,246
  • 11
  • 19
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)

    }

};
kairocks2002
  • 436
  • 1
  • 3
  • 15
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);
    }
}
sean
  • 1,644
  • 1
  • 15
  • 14