javascript - 按 id 分组对象数组
问题描述
我正在尝试遍历数组 ob 对象并将数组的项目分组到具有匹配 id 的新数组中:
API 示例:
api_array [
{id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
];
我正在努力实现这个结果:
result [
group_one [
{id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
]
group_two [
{id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
]
group_three [
{id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
]
group_four [
{id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
{id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
]
]
我实际上已经设法实现了它,但我认为它很疯狂,并且我确信这里有一个更好的实现是我所做的:
const createAddresses = (address) => {
let group_one= [],
group_two = [],
group_three = [],
group_four = [],
group = [];
debugger;
for(let i=0; i < address.length; i++) {
switch (address[i].id) {
case 1:
group_one.push(address[i]);
break;
case 2:
group_two.push(address[i]);
break;
case 3:
group_three.push(address[i]);
break;
case 4:
group_four.push(address[i]);
break;
default:
return address;
}
}
console.log('GROUP', group);
return group.push(group_one, group_two, group_three, group_four);
}
我真的不喜欢这个实现并尝试过这个:
const obj = address.reduce((acc, cur) => ({...acc, [cur.id]: cur}), {});
上面所做的与我疯狂的 for 循环函数相同,但它只为每个组添加最后一个元素,如下所示:
result [
0 [
{id: 1, postcode: 'xxx', street: 'xxx', city: 'xxx'},
]
1 [
{id: 2, postcode: 'xxx', street: 'xxx', city: 'xxx'},
]
2 [
{id: 3, postcode: 'xxx', street: 'xxx', city: 'xxx'},
]
3 [`enter code here`
{id: 4, postcode: 'xxx', street: 'xxx', city: 'xxx'},
]
]
但就像我提到的那样,我需要每个组中的所有元素,请提供任何建议。
解决方案
按 id 分组对象数组
//Create a javascript array of objects containing key value pairs id, post
var api_array = [
{id: 1, postcode: '10'},
{id: 1, postcode: '11'},
{id: 2, postcode: '20'},
{id: 2, postcode: '21'},
{id: 2, postcode: '22'},
{id: 3, postcode: '30'}
];
//result is a javascript array containing the groups grouped by id.
let result = [];
//javascript array has a method foreach that enumerates keyvalue pairs.
api_array.forEach(
r => {
//if an array index by the value of id is not found, instantiate it.
if( !result[r.id] ){
//result gets a new index of the value at id.
result[r.id] = [];
}
//push that whole object from api_array into that list
result[r.id].push(r);
}
);
console.log(result[1]);
console.log(result[2]);
console.log(result[3]);
印刷:
[ { id: 1, postcode: '10' }, { id: 1, postcode: '11' } ]
[ { id: 2, postcode: '20' },
{ id: 2, postcode: '21' },
{ id: 2, postcode: '22' } ]
[ { id: 3, postcode: '30' } ]
推荐阅读
- c++ - 不同容器上的 std::stack 实现有什么实际区别?
- python - 如何用另一个框架替换框架
- javascript - 为什么我收到内部服务器错误 500
- javascript - React Native Drawer 不会自动关闭
- search - Solr 同义词与预期不匹配
- angular - msal 角度更改范围为小写,在 1.3.4 中修复
- python-3.x - 如何在python中格式化列表中的多个项目
- openshift - Openshift - 如何获取 POD 列表的当前内存使用情况
- javascript - JavaScript 验证输入无法正常工作
- sql-server - 如何在 Python 中的 Pymmsql 中创建和重新创建索引?