admin管理员组文章数量:1332383
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 stationByDs100
:
{ 'OECSC': { name: 'A Coruna', ds100: 'OECSC' }, ... }
At the moment I do it as follows:
var stationByDs100 = {};
stations.forEach(station => stationByDs100[station.ds100] = station);
I'm looking for a better way to acplish this - is it possible with a one-liner without explicit variable declaration?
For instance in Java 8 this could have acplished with streams and collectors in one line like:
Map<String, Station> stationByDs100 = stations.stream()
.collect(toMap(Station::getDs100, identity()));
So I was thinking maybe there's a similar way in JS.
I'm using Node.js so OK to use the latest JS/ES features Node.js supports.
I've browsed through a rougly dozen existing answers but they mostly use older JS/ES versions and suggest even longer solutions.
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 stationByDs100
:
{ 'OECSC': { name: 'A Coruna', ds100: 'OECSC' }, ... }
At the moment I do it as follows:
var stationByDs100 = {};
stations.forEach(station => stationByDs100[station.ds100] = station);
I'm looking for a better way to acplish this - is it possible with a one-liner without explicit variable declaration?
For instance in Java 8 this could have acplished with streams and collectors in one line like:
Map<String, Station> stationByDs100 = stations.stream()
.collect(toMap(Station::getDs100, identity()));
So I was thinking maybe there's a similar way in JS.
I'm using Node.js so OK to use the latest JS/ES features Node.js supports.
I've browsed through a rougly dozen existing answers but they mostly use older JS/ES versions and suggest even longer solutions.
Share Improve this question asked Jan 29, 2017 at 10:09 lexicorelexicore 43.7k17 gold badges140 silver badges225 bronze badges 5- 1 Use Array.prototype.reduce, see MDN for examples. – RobG Commented Jan 29, 2017 at 10:11
- @SpencerWieczorek One-liner, without explicit variable declaration, from said criteria it is better. – lexicore Commented Jan 29, 2017 at 10:17
- Is there a specific purpose for wanting the code to be a one-liner? Usually one-line code, especially involving functional-style array methods, are slower than the equivalent code using for/while loops and more than one line of code – Zorgatone Commented Jan 29, 2017 at 10:19
- 1 @Zorgatone No technical reason. I don't care about performance in this case and wanted to learn more about functional style in JS as I'm quite weak in that. – lexicore Commented Jan 29, 2017 at 10:21
- Ok, makes sense – Zorgatone Commented Jan 29, 2017 at 10:22
2 Answers
Reset to default 9You could use Object.assign
with puted property names.
var stations = [ { name: 'A Coruna', ds100: 'OECSC' }],
object = stations.reduce((o, a) => Object.assign(o, { [a.ds100]: a }), {});
console.log(object);
Object.fromEntries
can help with this:
const stations = [{ name: 'Berlin Südkreuz', ds100: 'BPAF' }, { name: 'Berg', ds100: 'KBEG' }];
const object = Object.fromEntries(stations.map(o => [o.ds100, o]));
console.log(object);
本文标签: nodejsConvert array to object in JavaScriptStack Overflow
版权声明:本文标题:node.js - Convert array to object in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742297324a2448996.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论