我想对数组数组进行重复数据删除。复制数组是与元素索引的子集匹配的数组。在这种情况下,例如,索引[1]
和索引[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
一样有用,但到目前为止,我似乎找不到能够产生所需结果的结构。
答案 0 :(得分:2)
遍历行时,可以根据列中的值创建Set。您可以选择仅为指定的列创建集合,例如您的情况下为1和3。然后,在遍历每一行时,请检查该行中是否有任何指定的列具有已在对应集中的值,并且是否将其丢弃。
(在电话上,不能键入实际代码。我想代码也很简单)
答案 1 :(得分:1)
这可能不是最有效的算法,但我会做类似的事情
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;
}
答案 2 :(得分:0)
如果我对自己的工作了解得很好,那么这就是我所做的
mix.options({
extractVueStyles: true,
globalVueStyles: 'resources/sass/' + process.env.MIX_THEME + '/_variables.scss'
});
答案 3 :(得分:0)
效率不是最高,但这将删除多个重复数组的重复
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)
答案 4 :(得分:0)
这很简洁。它使用嵌套过滤器。它也适用于任意数量的重复项,仅保留第一个重复项。
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]));