[英]Concatenate Array of Objects into a new object depending on a key value pair
I have an Array of Objects so that depending on a certain key's value I'd like to create a new array of objects with that particular key.我有一个对象数组,因此根据某个键的值,我想用该特定键创建一个新的对象数组。
For eg, I have an array of Users例如,我有一组用户
{
association: "template"
color: "(255, 209, 0, 0.35)"
email: "ehe@example.com"
id: "12158_2"
name: "sevderadebvu"
rolename: "Floyd Phelps"
templateId: 8888
type: "template"
},
{
association: "template"
color: "(0, 7, 255, 0.35)"
email: "unoja@host.local"
id: "12158_3"
name: "ekdevajwirum"
rolename: "Virginia Martin"
templateId: 8888
type: "template"
},
{
association: "template"
color: "(255, 121, 169, 0.35)"
email: "nut@host.invalid"
id: "12112_4"
name: "Message Role 1"
rolename: "Bobby Barnes"
templateId: 1111
type: "template"
},
{
association: "template"
color: "(255, 12, 119, 0.35)"
email: "rnaonew@host.invalid"
id: "12112_4"
name: "Messi"
rolename: "Bobby Barnes"
templateId: 9999
type: "template"
}
]
Now, based on the templateId
I would like to create a new Array of objects that uses templateId as key and has all data corresponding to that templateId's Object as separate objects as shown below:现在,基于
templateId
,我想创建一个新的对象数组,它使用 templateId 作为键,并将与该 templateId 的 Object 对应的所有数据作为单独的对象,如下所示:
Expected Result预期结果
"users": {
"8888": [
{
association: "template"
color: "(255, 209, 0, 0.35)"
email: "ehe@example.com"
id: "12158_2"
name: "sevderadebvu"
rolename: "Floyd Phelps"
type: "template"
},
{
association: "template"
color: "(0, 7, 255, 0.35)"
email: "unoja@host.local"
id: "12158_3"
name: "ekdevajwirum"
rolename: "Virginia Martin"
type: "template"
}
],
"1111": [
{
association: "template"
color: "(255, 121, 169, 0.35)"
email: "nut@host.invalid"
id: "12112_4"
name: "Message Role 1"
rolename: "Bobby Barnes"
type: "template"
},
],
"9999": [
{
association: "template"
color: "(255, 12, 119, 0.35)"
email: "rnaonew@host.invalid"
id: "12112_4"
name: "Messi"
rolename: "Bobby Barnes"
type: "template"
}
]
}
}
Ignore any missing key-value pairs (templateId removed from the expected result)忽略任何缺失的键值对(templateId 从预期结果中移除)
You can use the function Array.prototype.reduce
for grouping by the templateId
as follow:您可以使用 function
Array.prototype.reduce
按templateId
进行分组,如下所示:
const arr = [{ association: "template", color: "(255, 209, 0, 0.35)", email: "ehe@example.com", id: "12158_2", name: "sevderadebvu", rolename: "Floyd Phelps", templateId: 8888, type: "template"},{ association: "template", color: "(0, 7, 255, 0.35)", email: "unoja@host.local", id: "12158_3", name: "ekdevajwirum", rolename: "Virginia Martin", templateId: 8888, type: "template"},{ association: "template", color: "(255, 121, 169, 0.35)", email: "nut@host.invalid", id: "12112_4", name: "Message Role 1", rolename: "Bobby Barnes", templateId: 1111, type: "template"},{ association: "template", color: "(255, 12, 119, 0.35)", email: "rnaonew@host.invalid", id: "12112_4", name: "Messi", rolename: "Bobby Barnes", templateId: 9999, type: "template"}], skipKeys = ["templateId", "rolename"], groupingKey = "templateId", result = arr.reduce((r, c) => { let desiredObject = Object.entries(c).filter(([key]) =>.skipKeys.includes(key)),reduce((a, [key. value]) => Object,assign(a: ({[key], value})); {}). (r[c[groupingKey]] || (r[c[groupingKey]] = []));push(desiredObject); return r, }. Object;create(null)). console;log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.