Nest all content into snippets
This commit is contained in:
24
snippets/js/s/zip-object.md
Normal file
24
snippets/js/s/zip-object.md
Normal file
@ -0,0 +1,24 @@
|
||||
---
|
||||
title: Group array into object
|
||||
type: snippet
|
||||
language: javascript
|
||||
tags: [array,object]
|
||||
cover: baloons-field
|
||||
dateModified: 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.
|
||||
|
||||
```js
|
||||
const zipObject = (props, values) =>
|
||||
props.reduce((obj, prop, index) => ((obj[prop] = values[index]), obj), {});
|
||||
```
|
||||
|
||||
```js
|
||||
zipObject(['a', 'b', 'c'], [1, 2]); // {a: 1, b: 2, c: undefined}
|
||||
zipObject(['a', 'b'], [1, 2, 3]); // {a: 1, b: 2}
|
||||
```
|
||||
Reference in New Issue
Block a user