在javascript中删除重复的数组数组

我想对数组数组进行重复数据删除。重复数组是匹配元素索引子集的数组。在这种情况下,比如说 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()可以给我这样的结果?


我已经尝试了以下功能,但这只是一个开始。它离给我想要的结果还差得远。但它让我知道我正在尝试做什么。


/**

 * 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 函数,_.filter但_.some到目前为止,我似乎无法找到产生所需结果的结构。


倚天杖
浏览 112回答 5
5回答

一只萌萌小番薯

您可以在迭代行时根据列中的值创建Set 。您可以选择仅为指定列创建集合,例如在您的情况下为 1 和 3。然后,在遍历每一行时,检查该行中的任何指定列是否具有已经在相应集合中的值,如果存在,则丢弃该行。(在手机上,无法输入实际代码。我猜代码也很简单)

蝴蝶不菲

这可能不是最有效的算法,但我会做类似的事情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;}

收到一只叮咚

如果我明白你想做什么,我知道,但这是我所做的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 numbersres_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)

叮当猫咪

不是最有效的,但这将删除多个重复数组的副本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 = [&nbsp; [ 11, 12, 13, 14, 15, ],&nbsp; [ 21, 22, 23, 24, 25, ],// duplicate in indices: 1, 3 with row index 3&nbsp; [ 31, 88, 33, 99, 35, ], // duplicate in indices: 1, 3 with row index 4&nbsp; [ 21, 22, 43, 24, 45, ],// duplicate in indices: 1, 3 // delete this&nbsp; [ 51, 88, 53, 99, 55, ], // duplicate in indices: 1, 3 // delete this row from result];function getDeduplicated(arr, arind) {&nbsp; for (let i = 0; i < arr.length; i++) {&nbsp; &nbsp; for (let j = 1 + i; j < arr.length; j++) {&nbsp; &nbsp; &nbsp; if (arr[j].includes(arr[i][arind[0]]) && arr[j].includes(arr[i][arind[1]])) {&nbsp; &nbsp; &nbsp; &nbsp; arr.splice(j, 1)&nbsp; &nbsp; &nbsp; &nbsp; i--&nbsp; &nbsp; &nbsp; } else continue&nbsp; &nbsp; }&nbsp; }&nbsp; return arr}const deduplicated = getDeduplicated(unDeduplicated, [1, 3]);const deduplicated2 = getDeduplicated(unDeduplicated1, [1, 3]);console.log(deduplicated)console.log("#####################")console.log(deduplicated2)

沧海一幻觉

这是相当简洁的。它使用嵌套过滤器。它也适用于任意数量的重复项,只保留第一个。init = [&nbsp; [ 11, 12, 13, 14, 15],&nbsp; [ 21, 22, 23, 24, 25],&nbsp; [ 31, 88, 33, 99, 35],&nbsp; [ 41, 42, 43, 44, 45],&nbsp; [ 51, 88, 53, 99, 55],];var deDuplicate = function(array, indices){var res = array.filter(&nbsp; (elem) => !array.some(&nbsp; (el) =>&nbsp; array.indexOf(el) < array.indexOf(elem) && //check that we don't discard the first dupe&nbsp; 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 ===&nbsp; ));return(res);}console.log(deDuplicate(init,[1,3]));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript