1

Possible Duplicate:
Javascript: Determine whether an array contains a value

var thelist = new Array();
function addlist(){
thelist.push(documentgetElementById('data').innerHTML);
}

How can I check that the data I push does not already exsist in array thelist?

Community
  • 1
  • 1
user1431627
  • 815
  • 3
  • 13
  • 30

4 Answers4

4
var thelist = []; // Use the array literal, not the constructor.
function addlist(){

  // get the data we want to make sure is unique
  var data = documentgetElementById('data').innerHTML;

  // make a flag to keep track of whether or not it exists.
  var exists = false;

  // Loop through the array
  for (var i = 0; i < thelist.length; i++) {

    // if we found the data in there already, flip the flag
    if (thelist[i] === data) {
      exists = true;

      // stop looping, once we have found something, no reason to loop more.
      break;
    }
  }

  // If the data doesn't exist yet, push it on there.
  if (!exists) {
    thelist.push(data);
  }
}
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • 1
    Might as well put a `break;` inside the `if` block that's inside the `for` loop – Ian Nov 16 '12 at 18:35
  • 1
    And use [`Array#indexOf`](http://ecma-international.org/ecma-262/5.1/#sec-15.4.4.14), as nearly all engines have it (use a polyfill for those that don't). In hopes that the native implementation might be faster than your loop (which is by no means guaranteed). – T.J. Crowder Nov 16 '12 at 18:37
  • @T.J.Crowder Yeah, that basically removes the whole `for` loop stuff and allows for a one/two liner. I was gonna post an answer with that, but then this answer was here that basically does that. – Ian Nov 16 '12 at 18:39
1

If you don't care about IE < 9 you could also use the Array method "some". Just have a look at this example:

var thelist = [1, 2, 3];

function addlist(data) {

    alreadyExists = thelist.some(function (item) {
        return item === data
    });

    if (!alreadyExists) {
        thelist.push(data);
    }
}
addlist(1);
addlist(2);
addlist(5);

console.log(thelist);​

http://jsfiddle.net/C7PBf/

Some determines whether at least one element with given constraint (callbacks return value === true) does exist or not.

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/some

Robin Drexler
  • 4,307
  • 3
  • 25
  • 28
0

If you are not concerned about IE version 8 or lower, you can use Array.filter:

var thelist = new Array();
function addlist(){
    var val = documentgetElementById('data').innerHTML;
    var isInArray = theList.filter(function(item){
        return item != val
    }).length > 0;

    if (!isInArray)
        thelist.push(val);
}

Or, you could use Array.indexOf:

var thelist = new Array();
function addlist(){
    var val = documentgetElementById('data').innerHTML;
    var isInArray = theList.indexOf(val) >= 0;

    if (!isInArray)
        thelist.push(val);
}
Shmiddty
  • 13,847
  • 1
  • 35
  • 52
0

Have a look at underscore.js : underscore.js Then you can check the array as

_.contains(thelist, 'value you want to check');

// The full example
var thelist = new Array();
function addlist(){
   var data = documentgetElementById('data').innerHTML;
   if(!_.contains(thelist, data)) theList.push(data);
}

or you can add the values to the array without concerning the duplicate values, and after the adding process is finished, you can remove the duplicate elements by

theList = _.uniq(theList);

The second method of course less efficient.

tolgaio
  • 3,206
  • 1
  • 19
  • 18
  • 2
    A whole library is overkill for a simple task such as this. – Shmiddty Nov 16 '12 at 18:42
  • True but the answer is still correct, and perhaps useful if there are other functionality in the OP code that could be simplified with a library. – Mark Thomas Nov 16 '12 at 19:35
  • @MarkThomas It is more simple and easy, but not very good if you want to learn JavaScript or prefer Vanilla JS (like me). – user1431627 Nov 16 '12 at 20:10