1

Okay, I've tried several examples found on stackoverflow on my example and nothing worked for me as I wanted. I have an array with 12 object in it which looks like this

var buttons = [ {"tag":"adr","color":"#123456"},
           {"tag":"ag","color":"#123456"},
           {"tag":"fax","color":"#123456"},
           {"tag":"ind","color":"#123456"},
           {"tag":"sname","color":"#123456"},
           {"tag":"per","color":"#123456"},
           {"tag":"tel","color":"#123456"},
           {"tag":"url","color":"#123456"},
           {"tag":"url","color":"#123456"},
           {"tag":"weblink","color":"#123456"},
           {"tag":"weblink","color":"#123456"},
           {"tag":"close","color":"#123456"} ];

and I wanna push this to another array var buttonsP if it is not in that array already. I tried (among other examples) with jquery .grep option:

var buttonsP =[];

for (var i = 0; i < buttons.length; i++){

    var newObject = { "valueTagP": buttons[i].tag, "colorTagP": buttons[i].color };    
    if( $.grep(buttonsP, function(obj) { return obj.valueTagP != buttons[i].tag; }) ){
        buttonsP.push(newObject);
    }
}

for (var i = 0; i < buttonsP.length; i++){
    $(".list").append("<li>"+ buttonsP[i].valueTagP + ", " + buttonsP[i].colorTagP +"</li>");
}

But duplicates were not removed.

You can check and edit my situation here: http://jsfiddle.net/M4mA9/

What am I doing wrong?

dzordz
  • 2,277
  • 13
  • 51
  • 74
  • 1
    @Cerbrus That did not work for me because I have objects in array, not values. I've tried to modify just that example but I lost myself – dzordz Jan 31 '14 at 12:53
  • Plz check http://stackoverflow.com/questions/6680430/get-unique-results-from-json-array-using-jquery – Bhavesh Parekh Jan 31 '14 at 12:57

4 Answers4

2

$.grep will return the matched array based on the condition. You see the details at http://api.jquery.com/jquery.grep/

Your code should be like

    var newObject = { "valueTagP": buttons[i].tag, "colorTagP": buttons[i].color };   
    var newArr = $.grep(buttonsP, function(obj) {             
        return obj.valueTagP === buttons[i].tag; 
    });
    if(newArr.length === 0) {
        buttonsP.push(newObject);
    }

I modified your code and updated the fiddle

Vinoth
  • 657
  • 6
  • 12
1

I am assuming buttonsP contains only buttons.

In that case,

instead of making buttons an array, make it an object as

var buttons = {key : "some_unique_key",
               value: /*put your buttons array here*/}

now buttonsP will contain buttons object (not an buttons array)

Now you can easily find buttons object in your array based on the key (make sure it is unique) and insert if it is not there.

schnill
  • 905
  • 1
  • 5
  • 12
1
var buttonsP = $.grep(buttons, function (obj, i) {
    var m = buttons.filter(function (v) {
        return v.tag === obj.tag;
    })[0];
    return $.inArray(m, buttons) === i;
});

http://jsfiddle.net/f22dv/

Ram
  • 143,282
  • 16
  • 168
  • 197
0

If you may use JQuery, try it:

var a1 = ["Cecilie", "Lone"];
var a2 = ["Cecilie", "Emil"];
var b = $.unique($.merge($.merge([], a1), a2));
iury
  • 118
  • 5
  • `$.unique()` only works for DOM elements. It's used by jQuery internally. – Ram Jan 31 '14 at 13:07
  • and yet, it works! http://jsfiddle.net/LfZ55/ – iury Jan 31 '14 at 13:14
  • Yes, your demo works, from `$.unique` documentation, "_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._" :) – Ram Jan 31 '14 at 13:17