Array.prototype.toObjectByProperty( element=>element.property )
T.J. Crowder
tj.crowder at farsightsoftware.com
Sat Aug 5 11:41:08 UTC 2017
On Sat, Aug 5, 2017 at 12:22 PM, Lachlan Hunt
<lachlan.hunt at lachy.id.au> wrote:
>
> On 2017-08-05 20:18, Naveen Chawla wrote:
>>
>> I've often needed to cache array elements by a unique key each
>> element has, for quick access.
>>
>
> That looks like what Array.prototype.map can already do.
No, the end result is an object, not an array. It probably would have
helped if Naveen had included an example. Here I'm using `Object.from` (see
my previous email) rather than `toObjectByProperty`:
```js
const a = [{id: "tjc", name: "T.J. Crowder"}, {id: "nc", name: "Naveen
Chawla"}, {id: "lh", name: "Lachlan Hunt"}];
const index = Object.from(a);
console.log(index);
```
would yield:
```
{
tjc: {id: "tjc", name: "T.J. Crowder"},
nc: {id: "nc", name: "Naveen Chawla"},
lh: {id: "lh", name: "Lachlan Hunt"}
}
```
-- T.J. Crowder
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170805/3d58101e/attachment.html>
More information about the es-discuss
mailing list