user4217999
user4217999

Reputation:

Remove Same values array Jquery

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

Answers (3)

Indranil Mondal
Indranil Mondal

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

Dibish
Dibish

Reputation: 9303

You can try

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

Upvotes: 1

Alex J
Alex J

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

Related Questions