I have a simple JSON with an array that contains further objects, etc. like this:
languagePack:
[
{
'key': 'Username',
'value': 'Benutzername',
'group': 'default'
},
{
'key': 'Password',
'value': 'Passwort',
'group': 'default'
}
]
But what I really want is an object like this:
languagePack:
{
'Username': 'Benutzername',
'Password': 'Passwort'
}
So, I want to reduce the array to simple key-value-pairs that are inside an array or even an object (keys are unique). Does anyone have an idea how to reduce this with some of these cool array functions? I only came up with something like an for each and building the object "by hand" property for property, but I remember there were some cool things for array like 'reduce', the spread operator (...), map, every, some, etc.
I tried it with something like:
var temp = this.languagePack.map(([key, value]) => ({key,value}))
console.log(temp)
But that only got me an error message TypeError: Invalid attempt to destructure non-iterable instance
Edit: All three answers are working perfectly fine. Thanks.
group
shall be ignored? – Bergi Nov 15 '19 at 17:07