Files
30-seconds-of-code/snippets/zipObject.md
2022-12-04 22:20:49 +02:00

772 B

title, tags, cover, firstSeen, lastUpdated
title tags cover firstSeen lastUpdated
Group array into object array,object blog_images/baloons-field.jpg 2017-12-21T00:55:18+02:00 2020-10-22T20:24:44+03:00

Associates properties to values, given array of valid property identifiers and an array of values.

  • Use Array.prototype.reduce() to build an object from the two arrays.
  • If the length of props is longer than values, remaining keys will be undefined.
  • If the length of values is longer than props, remaining values will be ignored.
const zipObject = (props, values) =>
  props.reduce((obj, prop, index) => ((obj[prop] = values[index]), obj), {});
zipObject(['a', 'b', 'c'], [1, 2]); // {a: 1, b: 2, c: undefined}
zipObject(['a', 'b'], [1, 2, 3]); // {a: 1, b: 2}