JavaScript Push distinct item in Jquery [duplicate]

Try following:

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;
}

var arr = [];

a.forEach( function(item) {
    if (!in_array(item, arr)) {
        arr.push(item);
    }
});

in_array reference: JavaScript equivalent of PHP's in_array()

It looks if an item is existing and if it's distinct (doesn't exist) it push the item into the array.

Or the jQuery solution mentioned inside the comments: Remove Duplicates from JavaScript Array

Which is following:

var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];
var uniqueNames = [];
$.each(names, function(i, el){
    if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
});

I posted the codes to ensure that someone finding this inside google finds some actual code and not just links that maybe are already down or closed.

Answer:1

What does the following line do? (flag)?flag=false:flag=true; In this piece of code: var flag = true; function resize() { if(flag) { document.getElementById("centerimg")....

What does the following line do? (flag)?flag=false:flag=true; In this piece of code: var flag = true; function resize() { if(flag) { document.getElementById("centerimg")....

Is there in the world any compiler for a simple functional language such as that defined on this article, or maybe just Scheme or even ML for itself? That is, a compiler that will analyze the code for ...

Is there in the world any compiler for a simple functional language such as that defined on this article, or maybe just Scheme or even ML for itself? That is, a compiler that will analyze the code for ...

I'm working on a site for a client where he needs a countdown timer to count down to 10pm two days from each site update. (or however many days he wishes to enter) The countdown functions perfectly in ...

I'm working on a site for a client where he needs a countdown timer to count down to 10pm two days from each site update. (or however many days he wishes to enter) The countdown functions perfectly in ...

I am using linux and phantomjs to test some javascript var page = require('webpage').create(); var fs = require('fs'); var address = system.args[1]; page.onConsoleMessage = ...

I am using linux and phantomjs to test some javascript var page = require('webpage').create(); var fs = require('fs'); var address = system.args[1]; page.onConsoleMessage = ...

  1. phantomjs command line example
  2. phantomjs command line arguments
  3. phantomjs command line
  4. phantomjs command line args
  5. phantomjs command line options
  6. phantomjs command line parameters
  7. phantomjs command line open url
  8. phantomjs command line pdf
  9. phantomjs command line timeout
  10. phantomjs.exe command line
  11. install phantomjs command line
  12. phantomjs linux command line
  13. exit phantomjs command line
  14. phantomjs windows command line
  15. phantomjs screenshot command line
  16. using phantomjs command line
  17. phantomjs proxy command line
  18. phantomjs config command line
  19. phantomjs pass arguments command line
  20. phantomjs run from command line