I have an array like this:
[
{id: 32, color: 'ff00dd'},
{id: 64, color: 'ab230b'},
{id: 102, color: '5f561d'}
]
How can I convert it to this format:
{ 32: 'ff00dd', 64: 'ab230b', 102: '5f561d'}
(LoDash is useable)
Thanks in advance!
I have an array like this:
[
{id: 32, color: 'ff00dd'},
{id: 64, color: 'ab230b'},
{id: 102, color: '5f561d'}
]
How can I convert it to this format:
{ 32: 'ff00dd', 64: 'ab230b', 102: '5f561d'}
(LoDash is useable)
Thanks in advance!
Without LoDash, a simple for loop can do this for us:
var data = [
{id: 32, color: 'ff00dd'},
{id: 64, color: 'ab230b'},
{id: 102, color: '5f561d'}
],
obj = {};
for (var i = 0; i < data.length; i++)
obj[data[i].id] = data[i].color;
console.log(obj);
Open your JavaScript console to see the result.
The output of the console logging in the above snippet is:
> Object {32: "ff00dd", 64: "ab230b", 102: "5f561d"}