簡體   English   中英

刪除相同值數組的jQuery

[英]Remove Same values array Jquery

如何刪除具有與第一個數組相同值的重復數組。 下面是我擁有的數組。 誰能幫我這個忙。

我需要刪除第二個重復數組,並僅顯示第一個數組。

JS:

arr = [ [10,20] , [10,20] ]

jQuery的唯一性僅適用於DOM元素,我認為您正在尋找的是下划線庫中的uniq,該庫可在http://underscorejs.org/#uniq中找到

你可以試試

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

嘗試這個:-

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM