-1

I'm building an application which involves the creation of an array of objects, similar to this:

var foo = [{
'foo' : 'foo1' 
},
{
'foo' : 'foo2' 
},
{
'foo' : 'foo3' 
}];

there's then an HTML form where the user fills in the values for new objects. When the form is submitted the new values are pushed to the array. what I want is an if/else statement which checks if the new object already exists in the array. So something like:

document.getElementById('form').addEventListener('submit',function(){
var newObject = {'foo' : input value goes here }
if (//Checks that newObject doesn't already exist in the array) {
    foo.push(newObject)
}
else {
//do nothing
}
});

It's also probably worth noting that I'm using Angular

Hello World
  • 1,102
  • 2
  • 15
  • 33

2 Answers2

1

You can use this approach:

You need:

  1. Understand how to compare 2 objects.
  2. Do it in cycle.

How to compare 2 objects.

One of the ways is:

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

Note, that comparing ojbects this way is not good:

Serializing objects merely to compare is terribly expensive and not guaranteed to be reliable

As cookie monster mentioned in comments to this post. I just suggested it, to achieve what you want. You can find better variant. You can find some beautiful answers here.

How to do it in cycle :D

In your case it will be:

function checkIfObjectExists(array, newObject) {
    var i = 0;
    for(i = 0; i < array.length; i++ ) {
        var object = array[i];
        if(JSON.stringify(object) === JSON.stringify(newObject)) 
        {
            return true;
        }   
    }
    return false;
}

Also, I added function, so you can use it in your code.

Now add this to your code:

if (checkIfObjectExists(foo, newObject)) {
    // objects exists, do nothing
}
else {
    foo.push(newObject);
}

DEMO

Community
  • 1
  • 1
Sharikov Vladislav
  • 7,049
  • 9
  • 50
  • 87
  • Serializing objects merely to compare is terribly expensive and not guaranteed to be reliable. Also we don't know if all properties should match or just one. And using `for-in` on an Array is generally considered a bad practice. And finally you're creating an implicit global variable in your function. – cookie monster May 07 '14 at 22:27
  • I just suggested way to serialize objects and provided link to source, where he can find another ways. +. Didn't know about `for-in` — why it is so; where I can read about it? I edited my answer; is it correct now? – Sharikov Vladislav May 07 '14 at 22:33
  • It's slower, it gives no absolute guarantees of order and if `Array.prototype` is extended, it'll hit those properties. So there are just too many strikes against it making it not worthwhile when there are no such issues when using a traditional `for` loop. But don't forget about `var` before `item`. That's where the implicit global is being created. – cookie monster May 07 '14 at 22:36
  • 1
    @cookiemonster, thanks for this explanation. Next time, I will use for (and will edit my answer now). Also, I edited problem with `item` variable. – Sharikov Vladislav May 07 '14 at 22:39
  • 1
    @cookiemonster also removed `for-in` cycle and added `for` cycle + mentioned about serializing objects. Thank you. – Sharikov Vladislav May 07 '14 at 22:43
0

You'd have to loop through the foo-array and check for any duplicates.

document.getElementById('form').addEventListener('submit',function(){
    var newObject = {'foo' : input value goes here }
    if (!isInArray(foo, newObject, 'foo')) {
        foo.push(newObject)
    }
});
function isInArray(arr, newObj, type) {
    var i, tempObj, result = false;
    for (i = 0; i < arr.length; i += 1) {
        tempObj = arr[i];
         if (tempObj[type] === newObj[type]) {
            result = true;
        }
    }
    return result;
}

It's easier and faster if your array doesn't contain objects. Then you simply can make the if-clause to be:

document.getElementById('form').addEventListener('submit',function(){
    var newString = "foo bar";
    if (foo.indexOf(newString) === -1) {
        foo.push(newString);
    }
});
Tohveli
  • 487
  • 5
  • 18