_.fromPairs
Returns an object composed from key-value pairs.
- // Underscore/Lodash
- _.fromPairs([['a', 1], ['b', 2]]);
- // => { 'a': 1, 'b': 2 }
- // Native
- const fromPairs = function(arr) {
- return arr.reduce(function(accumulator, value) {
- accumulator[value[0]] = value[1];
- return accumulator;
- }, {})
- }
- // Compact form
- const fromPairs = (arr) => arr.reduce((acc, val) => (acc[val[0]] = val[1], acc), {})
- fromPairs([['a', 1], ['b', 2]]);
- // => { 'a': 1, 'b': 2 }
Browser Support for Array.prototype.reduce()
✔ | ✔ | 3.0 ✔ | 9.0 ✔ | 10.5 ✔ | 4.0 ✔ |
当前内容版权归 you-dont-need 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 you-dont-need .