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.

0

2 Answers 2

-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.

Sign up to request clarification or add additional context in comments.

Comments

-4

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

2 Comments

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.
So basically $.unique()'s logic is based on HTML DOM node Elements, if you missed it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.