Skip to content Skip to sidebar Skip to footer

Convert Array To Object In Javascript

I'd like to convert an array into an object using one of the properties as key. For instance: var stations = [ { name: 'A Coruna', ds100: 'OECSC' }, ... ]; I'd like to get station

Solution 1:

You could use Object.assign with computed property names.

var stations = [ { name: 'A Coruna', ds100: 'OECSC' }],
    object = stations.reduce((o, a) =>Object.assign(o, { [a.ds100]: a }), {});

console.log(object);

Post a Comment for "Convert Array To Object In Javascript"