diff --git a/snippets/zipObject.md b/snippets/zipObject.md new file mode 100644 index 000000000..f9214e7aa --- /dev/null +++ b/snippets/zipObject.md @@ -0,0 +1,11 @@ +### zipObject + +Given an array of valid property identifiers and an array of values, return an object associating the properties to the values. + +Since an object can have undefined values but not undefined property pointers, the array of properties is used to decide the structure of the resulting object using `Array.reduce()`. + +```js +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} +``` diff --git a/tag_database b/tag_database index f3ad1b365..a0a06d2d0 100644 --- a/tag_database +++ b/tag_database @@ -118,3 +118,4 @@ validateEmail:utility validateNumber:utility without:array zip:array +zipObject:array