0

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] ]
1
  • Neither of those is valid JavaScript syntax. JS arrays don't have keys. Commented Dec 18, 2014 at 7:02

3 Answers 3

3

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

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

1 Comment

Underscore _.uniq works on simple array, not on array of arrays.
1

You can try

function arraysEqual(a1,a2) {
    return JSON.stringify(a1)==JSON.stringify(a2);
}

1 Comment

This will be used to check whether two arrays are equal or not. But here we want to check whether the array contains unique item or not.
0

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;
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.