1 | ;
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = keyValMap;
|
7 |
|
8 |
|
9 | /**
|
10 | * Creates a keyed JS object from an array, given a function to produce the keys
|
11 | * and a function to produce the values from each item in the array.
|
12 | *
|
13 | * const phoneBook = [
|
14 | * { name: 'Jon', num: '555-1234' },
|
15 | * { name: 'Jenny', num: '867-5309' }
|
16 | * ]
|
17 | *
|
18 | * // { Jon: '555-1234', Jenny: '867-5309' }
|
19 | * const phonesByName = keyValMap(
|
20 | * phoneBook,
|
21 | * entry => entry.name,
|
22 | * entry => entry.num
|
23 | * )
|
24 | *
|
25 | */
|
26 | function keyValMap(list, keyFn, valFn) {
|
27 | return list.reduce(function (map, item) {
|
28 | return map[keyFn(item)] = valFn(item), map;
|
29 | }, Object.create(null));
|
30 | } /**
|
31 | * Copyright (c) 2015-present, Facebook, Inc.
|
32 | *
|
33 | * This source code is licensed under the MIT license found in the
|
34 | * LICENSE file in the root directory of this source tree.
|
35 | *
|
36 | *
|
37 | */ |
\ | No newline at end of file |