Lodash zipObject in ES6 JavaScript

Before ES6 was fully implemented in browser and had spotty transpiling, Lodash was my "go to" utility library. While it is nice to have a lot of the functionality now natively available, there are still a lot of functions that I would use on a regular basis I'm finding I have to reimplement.

One of those functions is zipObject. It's similar to zip that is found in many functional languages except instead of pairing elements in two arrays into one common array, it pairs them in a JavaScript object by key and value.

const zipObject = (props, values) => {  
  return props.reduce((prev, prop, i) => {
    return Object.assign(prev, { [prop]: values[i] });
  }, {});

It's straight forward to use. For example, when called with zipObject(['a', 'b', 'c'], [1, 2, 3]), it would return { a: 1, b: 2, c: 3 }.

Bret Lowrey

Code is like a war - the best code is one never written.

Florida, USA