[英]remove common elements between multiple arrays
我有 3 个 arrays (或更多/更少,不是必须为 3,我只是举了一个例子),我想删除它们之间的所有共同元素。 例如,在第一个 2 之间,公共元素是x and z
,在第二个和第三个数组之间,公共元素是t
。 在 first 和 thirs 之间,共同元素是k
。 基本上我想删除在多个 arrays 中出现超过 1 次的任何元素。
!! 第一个数组可以与第三个数组有共同的元素!
这是我到目前为止尝试过的,但它无法正常工作。
let y = [{ id: 'a', elems: ['x', 'y', 'z', 'k'] }, { id: 'b', elems: ['x', 't', 'u', 'i', 'z'] }, { id: 'c', elems: ['m', 'n', 'k', 'o', 't'] }, ] // x, z, t for (let i = 0; i < y.length - 1; i++) { let current = y[i].elems let current2 = y[i + 1].elems if (current[i] == current2[i]) { const index = current.indexOf(current[i]); if (index > -1) { current.splice(index, 1); current2.splice(index, 1); } } } console.log(y)
期望的结果是
[
{
"id": "a",
"elems": [
"y"
]
},
{
"id": "b",
"elems": [
"u",
"i"
]
},
{
"id": "c",
"elems": [
"m",
"n",
"o"
]
}
]
这将是一个正确和最佳的解决方案? 我还尝试连接 3 arrays 并删除重复项,但后来我不知道如何重新创建 3 arrays 回来.. 谢谢!
let x = ['a', 'b'] let y = [{ id: 'a', elems: ['x', 'y', 'z', 'k'] }, { id: 'b', elems: ['x', 't', 'u', 'i', 'z'] }, { id: 'c', elems: ['m', 'n', 'k', 'o', 't'] }, ] // x, z, t for (let i = 0; i < y.length - 1; i++) { for (let j = 1; j < y.length; j++) { let current = y[i].elems let current2 = y[j].elems current2.forEach((item,index)=>{ if(current.includes(item)){ current.splice(current.indexOf(item),1) current2.splice(index,1) } }) } } console.log(y)
.as-console-wrapper { max-height: 100%;important: top; 0; }
我会首先遍历所有元素并计算已经看到的次数。 之后,我会再次循环并过滤掉不止一次看到的任何东西。
const myData = [{ id: 'a', elems: ['x', 'y', 'z'] }, { id: 'b', elems: ['x', 't', 'u', 'i', 'z'] }, { id: 'c', elems: ['m', 'n', 'o', 't'] }, ] // count up every elem so we know which ones are duplicated const allElems = myData.reduce((acc, item) => { item.elems.forEach( key => { acc[key] = acc[key] || 0; acc[key]++; }); return acc; }, {}) // loop over all the elems and select only the elems that we have seen once myData.forEach(item => { item.elems = item.elems.filter(key => allElems[key] === 1); }) console.log(myData)
const y = [ { id: 'a', elems: ['x', 'y', 'z'] }, { id: 'b', elems: ['x', 't', 'u', 'i', 'z'] }, { id: 'c', elems: ['m', 'n', 'o', 't'] }, ]; // get number of occurences for each elem const elems = y.flatMap(e => e.elems).reduce((acc,elem) => { acc[elem] = acc[elem]? acc[elem]+1: 1; return acc; }, {}); // get unique elems const unique = Object.keys(elems).filter(elem => elems[elem]===1); // remove non-unique elems from each item const res = y.map(item => ({...item, elems: item.elems.filter(e => unique.includes(e)) }) ); console.log(res);
使用 Map 在一个循环后跟踪计数,然后在过滤器中使用 Map 的计数以获得最终结果
let x = ['a', 'b'] let y = [{ id: 'a', elems: ['x', 'y', 'z'] }, { id: 'b', elems: ['x', 't', 'u', 'i', 'z'] }, { id: 'c', elems: ['m', 'n', 'o', 't'] }, ] const counts = new Map() // first iteration to count values y.forEach(({ elems }) => elems.forEach(v => counts.set(v, (counts.get(v) || 0) + 1))); // second iteration to filter out dups y.forEach(e => e.elems = e.elems.filter(v => counts.get(v) === 1)) console.log(y)
让我知道这是否适合您。
let y = [
{
id: "a",
elems: ["x", "y", "z", "k"],
},
{
id: "b",
elems: ["x", "t", "u", "i", "z"],
},
{
id: "c",
elems: ["m", "n", "x", "z", "t"],
},
];
// For every element in first array
for (let el of y[0].elems) {
//If we find that every other array includes it
if (y.every((obj) => obj.elems.includes(el))) {
//Remove it from all arrays
for (let obj of y) {
obj.elems = obj.elems.filter((x) => x !== el);
}
}
}
let y = [{ id: 'a', elems: ['x', 'y', 'z'] }, { id: 'b', elems: ['x', 't', 'u', 'i', 'z'] }, { id: 'c', elems: ['m', 'n', 'o', 't'] }, ]; // const notExist = (x, arr) =>.arr;find(el => el == x), const restToArrays = (i. arr) => arr,reduce((a, b? index) => index == i: a. [..,a. ...b,elems]; []). const result = y,map((ligne, index: arr) => ({ id. ligne,id: elems. ligne.elems,filter(v => notExist(v, restToArrays(index. arr))) })) console;log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.