第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在javascript中刪除重復(fù)的數(shù)組數(shù)組

在javascript中刪除重復(fù)的數(shù)組數(shù)組

倚天杖 2022-11-11 16:11:27
我想對數(shù)組數(shù)組進(jìn)行重復(fù)數(shù)據(jù)刪除。重復(fù)數(shù)組是匹配元素索引子集的數(shù)組。在這種情況下,比如說 index[1]和 index [3]。const unDeduplicated = [  [ 11, 12, 13, 14, 15, ],  [ 21, 22, 23, 24, 25, ],  [ 31, 88, 33, 99, 35, ], // duplicate in indices: 1, 3 with row index 4  [ 41, 42, 43, 44, 45, ],  [ 51, 88, 53, 99, 55, ], // duplicate in indices: 1, 3 // delete this row from result];const deduplicated = getDeduplicated( unDeduplicated, [ 1, 3, ], );console.log( deduplicated );// expected result:// [//   [ 11, 12, 13, 14, 15, ],//   [ 21, 22, 23, 24, 25, ],//   [ 31, 88, 33, 99, 35, ],//   [ 41, 42, 43, 44, 45, ],//   // this row was omitted from result because it was duplicated at indices 1 and 3 with row index 2// ]什么功能getDeduplicated()可以給我這樣的結(jié)果?我已經(jīng)嘗試了以下功能,但這只是一個開始。它離給我想要的結(jié)果還差得遠(yuǎn)。但它讓我知道我正在嘗試做什么。/** * Returns deduplicated array as a data grid ([][] -> 2D array) * @param { [][] } unDedupedDataGrid The original data grid to be deduplicated to include only unque rows as defined by the indices2compare. * @param { Number[] } indices2compare An array of indices to compare for each array element. * If every element at each index for a given row is duplicated elsewhere in the array, * then the array element is considered a duplicate * @returns { [][] } */const getDeduplicated = ( unDedupedDataGrid, indices2compare, ) => {  let deduped = [];  unDedupedDataGrid.forEach( row => {    const matchedArray = a.filter( row => row[1] === 88 && row[3] === 99 );    const matchedArrayLength = matchedArray.length;    if( matchedArrayLength ) return;    deduped.push( row, );  });}我研究了一些可能會有所幫助的 lodash 函數(shù),_.filter但_.some到目前為止,我似乎無法找到產(chǎn)生所需結(jié)果的結(jié)構(gòu)。
查看完整描述

5 回答

?
一只萌萌小番薯

TA貢獻(xiàn)1795條經(jīng)驗 獲得超7個贊

您可以在迭代行時根據(jù)列中的值創(chuàng)建Set 。您可以選擇僅為指定列創(chuàng)建集合,例如在您的情況下為 1 和 3。然后,在遍歷每一行時,檢查該行中的任何指定列是否具有已經(jīng)在相應(yīng)集合中的值,如果存在,則丟棄該行。

(在手機(jī)上,無法輸入實際代碼。我猜代碼也很簡單)


查看完整回答
反對 回復(fù) 2022-11-11
?
蝴蝶不菲

TA貢獻(xiàn)1810條經(jīng)驗 獲得超4個贊

這可能不是最有效的算法,但我會做類似的事情


function getDeduplicated(unDeduplicated, idxs) {

  const result = [];

  const used = new Set();

  unDeduplicated.forEach(arr => {

    const vals = idxs.map(i => arr[i]).join();

    if (!used.has(vals)) {

      result.push(arr);

      used.add(vals);

    }

  });


  return result;

}


查看完整回答
反對 回復(fù) 2022-11-11
?
收到一只叮咚

TA貢獻(xiàn)1821條經(jīng)驗 獲得超5個贊

如果我明白你想做什么,我知道,但這是我所做的


list = [

  [ 11, 12, 13, 14, 15, ],

  [ 21, 22, 23, 24, 25, ],

  [ 21, 58, 49, 57, 28, ],

  [ 31, 88, 33, 88, 35, ],

  [ 41, 42, 43, 44, 45, ],

  [ 51, 88, 53, 88, 55, ],

  [ 41, 77, 16, 29, 37, ],

];


el_list = []  // Auxiliar to save all unique numbers

res_list = list.reduce(

    (_list, row) => {

        // console.log(_list)

        this_rows_el = []  // Auxiliar to save this row's elements

        _list.push(row.reduce(

            (keep_row, el) => {

                // console.log(keep_row, this_rows_el, el)

                if(keep_row && el_list.indexOf(el)==-1 ){

                    el_list.push(el)

                    this_rows_el.push(el)

                    return true

                }else if(this_rows_el.indexOf(el)!=-1) return true  // Bypass repeated elements in this row

                else return false

            }, true) ? row : null)  // To get only duplicated rows (...) ? null : row )

        return _list

    }, []

)


console.log(res_list)


查看完整回答
反對 回復(fù) 2022-11-11
?
叮當(dāng)貓咪

TA貢獻(xiàn)1776條經(jīng)驗 獲得超12個贊

不是最有效的,但這將刪除多個重復(fù)數(shù)組的副本


const unDeduplicated = [ [ 11, 12, 13, 14, 15, ], [ 21, 22, 23, 24, 25, ], [ 31, 88, 33, 99, 35, ], [ 41, 33, 43, 44, 45, ], [ 51, 88, 53, 99, 55, ]]

const unDeduplicated1 = [

  [ 11, 12, 13, 14, 15, ],

  [ 21, 22, 23, 24, 25, ],// duplicate in indices: 1, 3 with row index 3

  [ 31, 88, 33, 99, 35, ], // duplicate in indices: 1, 3 with row index 4

  [ 21, 22, 43, 24, 45, ],// duplicate in indices: 1, 3 // delete this

  [ 51, 88, 53, 99, 55, ], // duplicate in indices: 1, 3 // delete this row from result

];

function getDeduplicated(arr, arind) {

  for (let i = 0; i < arr.length; i++) {

    for (let j = 1 + i; j < arr.length; j++) {

      if (arr[j].includes(arr[i][arind[0]]) && arr[j].includes(arr[i][arind[1]])) {

        arr.splice(j, 1)

        i--

      } else continue

    }

  }

  return arr

}

const deduplicated = getDeduplicated(unDeduplicated, [1, 3]);

const deduplicated2 = getDeduplicated(unDeduplicated1, [1, 3]);


console.log(deduplicated)

console.log("#####################")

console.log(deduplicated2)


查看完整回答
反對 回復(fù) 2022-11-11
?
滄海一幻覺

TA貢獻(xiàn)1824條經(jīng)驗 獲得超5個贊

這是相當(dāng)簡潔的。它使用嵌套過濾器。它也適用于任意數(shù)量的重復(fù)項,只保留第一個。


init = [

  [ 11, 12, 13, 14, 15],

  [ 21, 22, 23, 24, 25],

  [ 31, 88, 33, 99, 35],

  [ 41, 42, 43, 44, 45],

  [ 51, 88, 53, 99, 55],

];


var deDuplicate = function(array, indices){

var res = array.filter(

  (elem) => !array.some(

  (el) =>

  array.indexOf(el) < array.indexOf(elem) && //check that we don't discard the first dupe

  el.filter((i) => indices.includes(el.indexOf(i))).every((l,index) => l === elem.filter((j) => indices.includes(elem.indexOf(j)))[index])

//check if the requested indexes are the same.

// Made a bit nasty by the fact that you can't compare arrays with ===

  )

);

return(res);

}

console.log(deDuplicate(init,[1,3]));


查看完整回答
反對 回復(fù) 2022-11-11
  • 5 回答
  • 0 關(guān)注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號