Reputation:
How do I remove the repeated array which has the same values as the first array. Below is the array I have. Can anyone help me out on this.
I need to remove the repeated array which is second one and show only the 1st array.
JS:
arr = [ [10,20] , [10,20] ]
Upvotes: 0
Views: 95
Reputation: 2857
Try this:-
var arr = [ [10,20] , [30,20],[10,40],[10,20],[30,20] ],newArr=[];
$.each(arr, function(index,item){
if(searchForItem(newArr,item)<0){
newArr.push(item);
}
})
console.log(newArr);
function searchForItem(array, item){
var i, j, current;
for(i = 0; i < array.length; ++i){
if(item.length === array[i].length){
current = array[i];
for(j = 0; j < item.length && item[j] === current[j]; ++j);
if(j === item.length)
return i;
}
}
return -1;
}
Upvotes: 0
Reputation: 9303
You can try
function arraysEqual(a1,a2) {
return JSON.stringify(a1)==JSON.stringify(a2);
}
Upvotes: 1
Reputation: 1019
jQuery's unique only works on DOM elements, I think what you are looking for is the uniq from the underscore library, which can be found at http://underscorejs.org/#uniq
Upvotes: 3