简体   繁体   中英

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] ]

jQuery的唯一性仅适用于DOM元素,我认为您正在寻找的是下划线库中的uniq,该库可在http://underscorejs.org/#uniq中找到

You can try

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM