Javascript 将JSON数据数组分组到另一个数组
我正在构建一个Angular 7应用程序。 在这个应用程序中,我从服务器获取JSON数据,然后我希望通过特定的键对这些数据进行分组。我设法做到了这一点,但我不再有数组,而是某种对象 启动JSON数据Javascript 将JSON数据数组分组到另一个数组,javascript,angular,Javascript,Angular,我正在构建一个Angular 7应用程序。 在这个应用程序中,我从服务器获取JSON数据,然后我希望通过特定的键对这些数据进行分组。我设法做到了这一点,但我不再有数组,而是某种对象 启动JSON数据 [{ "id": 67, "survey_id": 26, "question_id": 63, "user_id": 35, "privacy": null, "score": null, "comment": "A nice little
[{
"id": 67,
"survey_id": 26,
"question_id": 63,
"user_id": 35,
"privacy": null,
"score": null,
"comment": "A nice little comment",
"binary": null,
"reasons": null,
"preferences": "{}",
"created_at_date": "2019-09-18",
"created_at_time": "19:53",
"user": {
"id": 35,
"fullname": "Michael Palin"
},
"can_manage": true
},
{
"id": 68,
"survey_id": 26,
"question_id": 64,
"user_id": 4,
"privacy": null,
"score": null,
"comment": "Another little comment",
"binary": null,
"reasons": null,
"preferences": "{}",
"created_at_date": "2019-09-18",
"created_at_time": "19:53",
"user": {
"id": 4,
"fullname": "Roland Smacker"
},
"can_manage": true
},
{
"id": 69,
"survey_id": 26,
"question_id": 65,
"user_id": 4,
"privacy": null,
"score": null,
"comment": "Some more comments",
"binary": null,
"reasons": null,
"preferences": "{}",
"created_at_date": "2019-09-18",
"created_at_time": "19:53",
"user": {
"id": 4,
"fullname": "Roland Smacker"
},
"can_manage": true
},
{
"id": 70,
"survey_id": 26,
"question_id": 66,
"user_id": 4,
"privacy": null,
"score": 5,
"comment": null,
"binary": null,
"reasons": null,
"preferences": "{}",
"created_at_date": "2019-09-18",
"created_at_time": "19:53",
"user": {
"id": 4,
"fullname": "Roland Smacker"
},
"can_manage": true
}
]
这是我的分组代码
setupStats() {
const groupBy = (array, key) => {
return array.reduce((result, currentValue) => {
(result[currentValue[key]] = result[currentValue[key]] || []).push(
currentValue
);
return result;
}, {});
};
const grouped = groupBy(this.collection, 'user_id');
console.log(grouped);
}
这是输出
{4: Array(3), 35: Array(1)}
4和35是用户ID。我想让它们得到数组的一部分,这样我就可以循环它们,这是我现在做不到的。您可以使用一个简单的循环来进行分组,类似这样:
const arr=[{
“id”:67,
“调查id”:26,
“评论”:“一个不错的小评论”,
“用户”:{
“id”:35,
“全名”:“迈克尔·佩林”
},
“你能管理”是真的吗
},
{
“id”:68,
“调查id”:26,
“用户”:{
“id”:4,
“全名”:“罗兰·斯帕克”
},
“你能管理”是真的吗
},
{
“id”:69,
“调查id”:26,
“用户”:{
“id”:4,
“全名”:“罗兰·斯帕克”
},
“你能管理”是真的吗
},
{
“id”:70,
“调查id”:26,
“用户”:{
“id”:4,
“全名”:“罗兰·斯帕克”
},
“你能管理”是真的吗
}];
常量映射={};
用于(arr的施工项目){
const id=item.user.id;
如果(!map[id]){
map[id]=[];
}
映射[id]。推送(项目);
}
//按id访问特定组。
//log(map['4']);
//在所有组上循环。
for(Object.entries(map))的const[id,groupArr]{
console.log('id:'+id);
log('associated array:',groupArr);
}
您可以使用一个简单的循环进行分组,如下所示:
const arr=[{
“id”:67,
“调查id”:26,
“评论”:“一个不错的小评论”,
“用户”:{
“id”:35,
“全名”:“迈克尔·佩林”
},
“你能管理”是真的吗
},
{
“id”:68,
“调查id”:26,
“用户”:{
“id”:4,
“全名”:“罗兰·斯帕克”
},
“你能管理”是真的吗
},
{
“id”:69,
“调查id”:26,
“用户”:{
“id”:4,
“全名”:“罗兰·斯帕克”
},
“你能管理”是真的吗
},
{
“id”:70,
“调查id”:26,
“用户”:{
“id”:4,
“全名”:“罗兰·斯帕克”
},
“你能管理”是真的吗
}];
常量映射={};
用于(arr的施工项目){
const id=item.user.id;
如果(!map[id]){
map[id]=[];
}
映射[id]。推送(项目);
}
//按id访问特定组。
//log(map['4']);
//在所有组上循环。
for(Object.entries(map))的const[id,groupArr]{
console.log('id:'+id);
log('associated array:',groupArr);
}
如果希望使用*ngFor
指令将分组数据绑定到模板,可以利用keyvalue
管道在分组对象上迭代:
{{group.key}{{group.value.length}}项
{{item.id}{{item.comment}
您可以阅读。如果希望使用
*ngFor
指令将分组数据绑定到模板,您可以利用keyvalue
管道在分组对象上迭代:
{{group.key}{{group.value.length}}项
{{item.id}{{item.comment}
您可以阅读。结果应该是什么样子的?您可以在“这是输出”上找到它,我已经看到了,但是您想要什么?结果应该是什么样子的?您可以在“这是输出”上找到它,我已经看到了,但是您想要什么?我需要按键分组(用户id)不是特定的ID。如何修改此代码以使用该ID?分组就是这样的。如果你愿意,你仍然可以在地图上循环,为什么你说你不能呢?你到底想实现什么?@MarkDenn请现在检查我的答案,我已经更新了代码以循环所有组。我需要根据密钥(用户id)而不是特定id进行分组。我如何修改此代码以使用它?分组就是这样的。如果你愿意,你仍然可以在地图上循环,为什么你说你不能呢?你到底想实现什么?@MarkDenn现在请检查我的答案,我已经更新了代码以循环所有组。这非常有效!我不知道keyvalue。非常感谢。伟大的很高兴它起到了作用。这非常有效!我不知道keyvalue。非常感谢。伟大的很高兴这有帮助。