我有一个数组对象的数组这是一个多维数组我需要删除所有数组中不常见的对象
这是多维数组:
const array111 = [
[{
name: 'test1',
parameters: {
employee_number: 1
}
},
{
name: 'test2',
parameters: {
employee_number: 2
}
}, {
name: 'test3',
parameters: {
employee_number: 4
}
},
{
name: 'test4',
parameters: {
employee_number: 5
}
},
{
name: 'test5',
parameters: {
employee_number: 6
}
}
],
[{
name: 'test1',
parameters: {
employee_number: 1
}
},
{
name: 'test4',
parameters: {
employee_number: 5
}
},
{
name: 'test5',
parameters: {
employee_number: 6
}
}
],
[{
name: 'test1',
parameters: {
employee_number: 1
}
},
{
name: 'test4',
parameters: {
employee_number: 4
}
},
{
name: 'test4',
parameters: {
employee_number: 5
}
},
{
name: 'test5',
parameters: {
employee_number: 6
}
}
],
[{
name: 'test1',
parameters: {
employee_number: 1
}
},
{
name: 'test4',
parameters: {
employee_number: 5
}
},
{
name: 'test5',
parameters: {
employee_number: 6
}
}
],
]
resultss = Object.values(array111.reduce((r, o) => {
key = []
key.push([...o.map(({
parameters
}) => parameters.employee_number)])
r.push(key)
return r;
}, []));
data = resultss.map(JSON.stringify).reverse().filter((e, i, a) => {
return a.indexOf(e, i) === 1;
}).reverse().map(JSON.parse)
filteredRes = array111.map((element) => {
element = element.filter(({
parameters
}) => data[0][0].some((val) => val === parameters.employee_number));
return element;
})
console.log(filteredRes)
在上面的数组employee_number "1,5,6"在所有对象数组和employee_number"2,4"应该被删除,这在所有数组中并不常见
我尝试的解决方案是给出正确的结果,但由于它是较长的方式,我想要较小的解决方案。
const array111 = [
[{
name: 'test1',
parameters: {
employee_number: 1
}
},
{
name: 'test4',
parameters: {
employee_number: 5
}
},
{
name: 'test5',
parameters: {
employee_number: 6
}
}
],
[{
name: 'test1',
parameters: {
employee_number: 1
}
},
{
name: 'test4',
parameters: {
employee_number: 5
}
},
{
name: 'test5',
parameters: {
employee_number: 6
}
}
],
[{
name: 'test1',
parameters: {
employee_number: 1
}
},
{
name: 'test4',
parameters: {
employee_number: 5
}
},
{
name: 'test5',
parameters: {
employee_number: 6
}
}
],
[{
name: 'test1',
parameters: {
employee_number: 1
}
},
{
name: 'test4',
parameters: {
employee_number: 5
}
},
{
name: 'test5',
parameters: {
employee_number: 6
}
}
],
]
以上是O/p
您可以使用Set和Map轻松实现结果
使用Set和Map
const array111 = [
[
{
name: "test1",
parameters: {
employee_number: 1,
},
},
{
name: "test2",
parameters: {
employee_number: 2,
},
},
{
name: "test3",
parameters: {
employee_number: 4,
},
},
{
name: "test4",
parameters: {
employee_number: 5,
},
},
{
name: "test5",
parameters: {
employee_number: 6,
},
},
],
[
{
name: "test1",
parameters: {
employee_number: 1,
},
},
{
name: "test4",
parameters: {
employee_number: 5,
},
},
{
name: "test5",
parameters: {
employee_number: 6,
},
},
],
[
{
name: "test1",
parameters: {
employee_number: 1,
},
},
{
name: "test4",
parameters: {
employee_number: 5,
},
},
{
name: "test5",
parameters: {
employee_number: 6,
},
},
],
[
{
name: "test1",
parameters: {
employee_number: 1,
},
},
{
name: "test4",
parameters: {
employee_number: 4,
},
},
{
name: "test4",
parameters: {
employee_number: 5,
},
},
{
name: "test5",
parameters: {
employee_number: 6,
},
},
],
];
const uniqueNamesFrequency = array111
.map((arr) => Array.from(arr.reduce((acc, curr) => acc.add(curr.parameters.employee_number),new Set())))
.reduce((acc, curr, i) => {
curr.forEach((id) => acc.set(id, (acc.get(id) ?? 0) + 1));
return acc;
}, new Map());
const result = array111.map((arr) =>arr.filter((o) => uniqueNamesFrequency.get(o.parameters.employee_number) === array111.length));
console.log(result);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }
不使用Set
const array111 = [
[
{
name: "test1",
parameters: {
employee_number: 1,
},
},
{
name: "test2",
parameters: {
employee_number: 2,
},
},
{
name: "test3",
parameters: {
employee_number: 4,
},
},
{
name: "test4",
parameters: {
employee_number: 5,
},
},
{
name: "test5",
parameters: {
employee_number: 6,
},
},
],
[
{
name: "test1",
parameters: {
employee_number: 1,
},
},
{
name: "test4",
parameters: {
employee_number: 5,
},
},
{
name: "test5",
parameters: {
employee_number: 6,
},
},
],
[
{
name: "test1",
parameters: {
employee_number: 1,
},
},
{
name: "test4",
parameters: {
employee_number: 5,
},
},
{
name: "test5",
parameters: {
employee_number: 6,
},
},
],
[
{
name: "test1",
parameters: {
employee_number: 1,
},
},
{
name: "test4",
parameters: {
employee_number: 4,
},
},
{
name: "test4",
parameters: {
employee_number: 5,
},
},
{
name: "test5",
parameters: {
employee_number: 6,
},
},
],
];
const uniqueNamesFrequency = array111
.map((arr) => Array.from( arr.reduce((acc, curr) => {
if (!acc.includes(curr.parameters.employee_number))
acc.push(curr.parameters.employee_number);
return acc;
}, [])))
.reduce((acc, curr, i) => {
curr.forEach((id) => acc.set(id, (acc.get(id) ?? 0) + 1));
return acc;
}, new Map());
const result = array111.map((arr) => arr.filter( (o) => uniqueNamesFrequency.get(o.parameters.employee_number) === array111.length));
console.log(result);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }
如果你不介意lodash图书馆:
const _ = require('lodash');
const uniqOnly = (arr) => {
const uniqPart = _.intersectionBy(...arr, (o) => o.parameters.employee_number);
return = arr.map(() => [...commomPart]);
}
console.log(uniqOnly(array111))
// output:
[
[
{ name: 'test1', parameters: [Object] },
{ name: 'test4', parameters: [Object] },
{ name: 'test5', parameters: [Object] }
],
[
{ name: 'test1', parameters: [Object] },
{ name: 'test4', parameters: [Object] },
{ name: 'test5', parameters: [Object] }
],
[
{ name: 'test1', parameters: [Object] },
{ name: 'test4', parameters: [Object] },
{ name: 'test5', parameters: [Object] }
],
[
{ name: 'test1', parameters: [Object] },
{ name: 'test4', parameters: [Object] },
{ name: 'test5', parameters: [Object] }
]
]