如何根据 属性 对对象进行分组
How to group objects according to a property
希望有人能帮助我,
情况:
我正在开发一个单元报告,其中我 select 我所有的客户和他们的 unit_id (unit_id 是与单元的关系,他们可以在哪里找到)和这个 select是一组对象数组,我在如何根据一个特定的 属性 对对象进行分组时遇到了问题,我有一个数组,如:
const clients = [
[
{
ID: 1,
NAME: 'John Doe',
UNITS: ['02'],
}
],
[
{
ID: 2,
NAME: 'Jane Doe',
UNITS: ['01', '02'],
}
],
[
{
ID: 3,
NAME: 'Doe John',
UNITS: ['50', '15'],
}
],
[
{
ID: 4,
NAME: 'Doe Jane',
UNITS: ['30'],
}
],
[
{
ID: 5,
NAME: 'Doe Jane',
UNITS: ['15'],
}
],
]
问题:
我需要用相同的“UNIT”对每个人进行分组,但我不能让多个组具有与我相同的“UNIT ID”在一组中,我期待这样的结果:
const unitGroups = [
[
{
UNIT_IDS: ['01', '02'],
CLIENTS: [
{
ID: 1,
NAME: 'John Doe',
},
{
ID: 2,
NAME: 'Jane Doe',
}
]
}
],
[
{
UNIT_IDS: ['50', '15'],
CLIENTS: [
{
ID: 3,
NAME: 'Doe John',
},
{
ID: 5,
NAME: 'Doe Jane',
}
]
}
],
[
{
UNIT_IDS: ['30'],
CLIENTS: [
{
ID: 4,
NAME: 'Doe Jane',
}
]
}
]
]
您可以使用 reduce
来获得结果。这是一个实现:
const clients = [ [ { ID: 1, NAME: 'John Doe', UNITS: ['02'], } ], [ { ID: 2, NAME: 'Jane Doe', UNITS: ['01', '02'], } ], [ { ID: 3, NAME: 'Doe John', UNITS: ['50', '15'], } ], [ { ID: 4, NAME: 'Doe Jane', UNITS: ['30'], } ], [ { ID: 5, NAME: 'Doe Jane', UNITS: ['15'], } ]];
const result = clients.reduce((a,e)=>{
e.forEach(({UNITS, ...rest})=>{
const getIndex = a.findIndex(p=>p.UNIT_IDS.some(b=>UNITS.some(c=>c===b)));
if(getIndex===-1){
const newData = {UNIT_IDS:UNITS, CLIENTS:[].concat(rest)};
a.push(newData);
} else {
a[getIndex].UNIT_IDS = [...new Set([...a[getIndex].UNIT_IDS, ...UNITS])];
a[getIndex].CLIENTS = [...a[getIndex].CLIENTS, rest]
}
})
return a;
},[]);
console.log(result);
const intersection = (array1, array2) =>
array1.filter((value) => array2.includes(value));
const results = [];
for (const { ID, NAME, UNITS } of clients.flat()) {
const contain = results.find(({ UNIT_IDS }) =>
intersection(UNIT_IDS, UNITS).length
);
if (contain) {
contain.CLIENTS.push({ ID, NAME });
if (UNITS.length > contain.UNIT_IDS.length) {
contain.UNIT_IDS = UNITS;
}
continue;
}
results.push({
UNIT_IDS: UNITS,
CLIENTS: [
{ ID, NAME },
],
});
}
console.log(results.map((group) => [group]));
希望有人能帮助我,
情况: 我正在开发一个单元报告,其中我 select 我所有的客户和他们的 unit_id (unit_id 是与单元的关系,他们可以在哪里找到)和这个 select是一组对象数组,我在如何根据一个特定的 属性 对对象进行分组时遇到了问题,我有一个数组,如:
const clients = [
[
{
ID: 1,
NAME: 'John Doe',
UNITS: ['02'],
}
],
[
{
ID: 2,
NAME: 'Jane Doe',
UNITS: ['01', '02'],
}
],
[
{
ID: 3,
NAME: 'Doe John',
UNITS: ['50', '15'],
}
],
[
{
ID: 4,
NAME: 'Doe Jane',
UNITS: ['30'],
}
],
[
{
ID: 5,
NAME: 'Doe Jane',
UNITS: ['15'],
}
],
]
问题: 我需要用相同的“UNIT”对每个人进行分组,但我不能让多个组具有与我相同的“UNIT ID”在一组中,我期待这样的结果:
const unitGroups = [
[
{
UNIT_IDS: ['01', '02'],
CLIENTS: [
{
ID: 1,
NAME: 'John Doe',
},
{
ID: 2,
NAME: 'Jane Doe',
}
]
}
],
[
{
UNIT_IDS: ['50', '15'],
CLIENTS: [
{
ID: 3,
NAME: 'Doe John',
},
{
ID: 5,
NAME: 'Doe Jane',
}
]
}
],
[
{
UNIT_IDS: ['30'],
CLIENTS: [
{
ID: 4,
NAME: 'Doe Jane',
}
]
}
]
]
您可以使用 reduce
来获得结果。这是一个实现:
const clients = [ [ { ID: 1, NAME: 'John Doe', UNITS: ['02'], } ], [ { ID: 2, NAME: 'Jane Doe', UNITS: ['01', '02'], } ], [ { ID: 3, NAME: 'Doe John', UNITS: ['50', '15'], } ], [ { ID: 4, NAME: 'Doe Jane', UNITS: ['30'], } ], [ { ID: 5, NAME: 'Doe Jane', UNITS: ['15'], } ]];
const result = clients.reduce((a,e)=>{
e.forEach(({UNITS, ...rest})=>{
const getIndex = a.findIndex(p=>p.UNIT_IDS.some(b=>UNITS.some(c=>c===b)));
if(getIndex===-1){
const newData = {UNIT_IDS:UNITS, CLIENTS:[].concat(rest)};
a.push(newData);
} else {
a[getIndex].UNIT_IDS = [...new Set([...a[getIndex].UNIT_IDS, ...UNITS])];
a[getIndex].CLIENTS = [...a[getIndex].CLIENTS, rest]
}
})
return a;
},[]);
console.log(result);
const intersection = (array1, array2) =>
array1.filter((value) => array2.includes(value));
const results = [];
for (const { ID, NAME, UNITS } of clients.flat()) {
const contain = results.find(({ UNIT_IDS }) =>
intersection(UNIT_IDS, UNITS).length
);
if (contain) {
contain.CLIENTS.push({ ID, NAME });
if (UNITS.length > contain.UNIT_IDS.length) {
contain.UNIT_IDS = UNITS;
}
continue;
}
results.push({
UNIT_IDS: UNITS,
CLIENTS: [
{ ID, NAME },
],
});
}
console.log(results.map((group) => [group]));