759 B
759 B
title, type, language, tags, cover, dateModified
| title | type | language | tags | cover | dateModified | ||
|---|---|---|---|---|---|---|---|
| Group array into object | snippet | javascript |
|
snowy-mountains | 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
propsis longer thanvalues, remaining keys will beundefined. - If the length of
valuesis longer thanprops, 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}