_.fromPairs

Returns an object composed from key-value pairs.

  1. // Underscore/Lodash
  2. _.fromPairs([['a', 1], ['b', 2]]);
  3. // => { 'a': 1, 'b': 2 }
  4.  
  5. // Native
  6. const fromPairs = function(arr) {
  7. return arr.reduce(function(accumulator, value) {
  8. accumulator[value[0]] = value[1];
  9. return accumulator;
  10. }, {})
  11. }
  12.  
  13. // Compact form
  14. const fromPairs = (arr) => arr.reduce((acc, val) => (acc[val[0]] = val[1], acc), {})
  15.  
  16. fromPairs([['a', 1], ['b', 2]]);
  17. // => { 'a': 1, 'b': 2 }

Browser Support for Array.prototype.reduce()

ChromeEdgeFirefoxIEOperaSafari
3.0 ✔9.0 ✔10.5 ✔4.0 ✔