Array.prototype.toObjectByProperty( element=>element.property )
Lachlan Hunt
lachlan.hunt at lachy.id.au
Sat Aug 5 12:05:27 UTC 2017
On 2017-08-05 21:47, T.J. Crowder wrote:
> On Sat, Aug 5, 2017 at 12:41 PM, T.J. Crowder
> <tj.crowder at farsightsoftware.com> wrote:
>
> Apologies, better proofreading required, left out the `"id"` argument:
>
> ```js
> const a = [{id: "tjc", name: "T.J. Crowder"}, {id: "nc", name: "Naveen
> Chawla"}, {id: "lh", name: "Lachlan Hunt"}];
> const index = Object.from(a, "id");
> console.log(index);
> ```
OK, I misunderstood the original request. But even so, as you noted in
your previous mail, Array.prototype.reduce can handle it.
a.reduce((obj, value) => (obj[value.id] = value, obj), {})
But basically, is the proposal for a native version of Lodash's
_.keyBy() method?
https://lodash.com/docs/#keyBy
--
Lachlan Hunt
https://lachy.id.au/
More information about the es-discuss
mailing list