I want to remove duplicates in my array with jQuery
The array
[[["Time",0],["Budget",1],["Scope",2],["Technical",3],["Budget",1]]]
If there is a name ("Budget") in this example which appears several times, then I want to remove this value pair.
I tried a lot but nothing worked so far. I tried to iterate trough the array and set a duplicate variable and just add the items with no duplicates to a new array
var items = new Array ();
// get the data with ajax request
$.ajax({
url : "localhost/..",
dataType : 'json',
success: function(data) {
for (var prop_name in data.items) {
var duplicate = 0;
var count = 0;
// count how often the value appears
for (i = 0; i < data.items.length; i++) {
if (data.items[prop_name] == data.items[i]){
var count = count+1
}
}
// if there are duplicates, just add the value once
for (i = 0; i < items.length; i++) {
if (data.items[prop_name] == data.items[i]){
duplicate = duplicate+1;
}
if (duplicate <= 1){
items.push([ data.items[prop_name], count])
}
duplicate = 0;
}
}
data.itemsis an array of an array of arrays? It seems like you have one pair of brackets too many. Can you provide the output ofconsole.log(JSON.stringify(data.items))to be sure about this?