Javascript 按id对对象数组进行分组
我正在尝试循环遍历数组ob对象,并将数组中的项分组到具有匹配id的新数组中: API示例:Javascript 按id对对象数组进行分组,javascript,Javascript,我正在尝试循环遍历数组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'},
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);
}
上面所做的与我的疯狂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
进行分组,并从映射中获取值作为结果集
结果集具有与所需密钥相同的顺序
函数分组依据(数组、键){
返回Array.from(数组
.reduce((m,o)=>m.set(o[key],…(m.get(o[key])| |[]),o]),新映射)
.values()
);
}
var data=[{id:1,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:1,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:2,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:2,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:2,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:3,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:3,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:3,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:4,邮政编码:'xxx',街道:'xxx'},{id:4,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{,
分组=分组依据(数据“id”);
console.log(分组);
.as console wrapper{max height:100%!important;top:0;}
您可以对相同的id
进行分组,并从映射中获取值作为结果集
结果集具有与所需密钥相同的顺序
函数分组依据(数组、键){
返回Array.from(数组
.reduce((m,o)=>m.set(o[key],…(m.get(o[key])| |[]),o]),新映射)
.values()
);
}
var data=[{id:1,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:1,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:2,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:2,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:2,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:3,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:3,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:3,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:4,邮政编码:'xxx',街道:'xxx'},{id:4,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{,
分组=分组依据(数据“id”);
console.log(分组);
.as console wrapper{max height:100%!important;top:0;}
类似于您的reduce
示例,它迭代数据并使用对象ID作为键创建一个对象并将其分组。然后它从该对象获取值
const-api_数组=[{id:1,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:1,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:2,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:2,邮政编码:'xxx',城市:'xxx'},{,{id:3,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:3,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:3,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:4,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:4,邮政编码:'xxx',街道:'xxx',城市:'xxx'};
const out=Object.values(api_数组.reduce((acc,c)=>{
常数{id}=c;
//如果对象没有与id匹配的键
//创建一个数组作为其值,然后对当前对象进行压缩
//否则,如果密钥仅存在于当前对象处,则
//到现有阵列
acc[id]=(acc[id]| |[])。concat(c);
返回acc;
}, {}));
console.log(out)
类似于您的reduce
示例,它迭代数据并使用对象ID作为键创建一个对象,并将它们分组在一起。然后它从该对象获取值
const-api_数组=[{id:1,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:1,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:2,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:2,邮政编码:'xxx',城市:'xxx'},{,{id:3,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:3,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:3,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:4,邮政编码:'xxx',街道:'xxx',城市:'xxx'},{id:4,邮政编码:'xxx',街道:'xxx',城市:'xxx'};
const out=Object.values(api_数组.reduce((acc,c)=>{
常数{id}=c;
//如果对象没有与id匹配的键
//创建一个数组作为其值,然后对当前对象进行压缩
//否则,如果密钥仅存在于当前对象处,则
//到现有阵列
acc[id]=(acc[id]| |[])。concat(c);
返回acc;
}, {}));
控制台。注销)
var objs=[
{id:1,邮政编码:“xxx”,街道:“xxx”,城市:“xxx”},
{id:1,邮政编码:“xxx”,街道:“xxx”,城市:“xxx”},
{id:2,邮政编码:“xxx”,街道:“xxx”,城市:“xxx”},
{id:3,邮政编码:“xxx”,街道:“xxx”,城市:“xxx”}
];
var result=objs.reduce(函数(r,a){
r[a.id]=r[a.id]| |[];
r[a.id].push(a);
返回r;
},Object.create(null));
console.log(结果);
var objs=[
{id:1,邮政编码:“xxx”,街道:“xxx”,城市:“xxx”},
{id:1,邮政编码:“xxx”,街道:“xxx”,城市:“xxx”},
{id:2,邮政编码:“x”
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'},
]
]
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'},
];
let result = [];
for (let i = 0; i < numberOfGroupsIWantToMake; i++) {
let newGroupArray = api_array.filter(obj => obj.id === i);
result.push(newGroupArray);
}
return result;
let 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'},
];
let grouped = groupArray(api_array);
console.log(grouped);
console.log(grouped[1]);
function groupArray(myArray) {
let grouped = {};
for (let i = 0; i < myArray.length; i++) {
let row = myArray[i];
let group = grouped[row.id];
if (!group) {
group = [];
grouped[row.id] = group;
}
group.push(row);
}
return grouped;
}
//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' } ]
var result = new Array(4);
api_array.forEach(function(item, index){
if (!result[item.id]){
result[item.id] = [];
}
result[item.id].push(item);
})