I am trying to transform an array of objects into an object with keys that are based on one of the properties of each object. For example:
Here is a sample of the original array:
const myArray = [
{"groupName":"groupname","description":"nice description here","name":"name1","value":107},
{"groupName":"groupname","description":"nice description here","name":"name1","value":107}
]
While Object.assign comes close to what I need, it assigns numeric keys to the objects like this:
let newObject = Object.assign({}, myArray);
This results in an object like this:
{
"0": {"groupName":"groupname","description":"nice description here","name":"name1","value":107},
"1": {"groupName":"groupname","description":"nice description here","name":"name1","value":107}
}
What I really want is for one of the properties to be used as the key instead of numbers, like this:
{
"name1": {"groupName":"groupname","description":"nice description here","name":"name1","value":107},
"name2": {"groupName":"groupname","description":"nice description here","name":"name2","value":107}
}
Although I can iterate and create the object manually, I wonder if there is a way to achieve this using ES6 without having to use forEach or map.
Any suggestions?