0

I started with an array formatted like this:

var cus = {
  "acct":[
    {
      "latitude":"41.4903",
      "longitude":"-90.56956",
      "part_no":"P1140",
      "no_sold":1
    },
    {
      "latitude":"48.118625",
      "longitude":"-96.1793",
      "part_no":"227",
      "no_sold":1
    },
    ....
  ]

Next I put all of the part_no in a separate array like this:

var list = [];
$.each(cus.acct,function(index,value){
  list = [value["part_no"]];

These are the results when I do a console.log() of my array:

["P1140"]
["227"]
["224"]
["600"]
.....
["756"]
["756"]
["756"]

How do I remove duplicates from this array of just part_no's with javascript/jquery? I've looked at other examples but can't find one that works for me. Take note that I'm just beginning with javascript as well.

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
M. Prince
  • 1
  • 1

2 Answers2

-1
function getUnique(arr){
var result = [];
$.each(arr, function(i, e) {

    if(typeof e != "undefined")
    {
        if ($.inArray(e, result) == -1) result.push(e)
    }
});
return result;

}

If you can use any libraries like underscope or lodash will provide more options.

Naresh217
  • 410
  • 1
  • 6
  • 19
-4

I would use the jQuery unique option. It should remove any duplicates from your array.

John Barton
  • 155
  • 2
  • 8
  • 1
    You should read the documentations: *Description: Sorts an array of DOM elements, in place, with the duplicates removed. Note that this only works on arrays of DOM elements, not strings or numbers.* – Roko C. Buljan Aug 10 '15 at 18:41
  • So basically `$.unique()`'s logic is based on HTML DOM node Elements, if you missed it – Roko C. Buljan Aug 10 '15 at 18:42