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:
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)