Files
30-seconds-of-code/snippets/select.md
Angelos Chalaris 851806a343 Made select variadic
2018-01-11 20:17:25 +02:00

17 lines
582 B
Markdown

### select
Retrieve a set of properties indicated by the given selectors from an object.
Use `Array.map()` for each selector, `String.split('.')` to split each selector and `Array.reduce()` to get the value indicated by it.
```js
const select = (from, ...selectors) =>
[...selectors].map(s => s.split('.').reduce((prev, cur) => prev && prev[cur], from));
```
```js
const obj = { selector: { to: { val: 'val to select' } } };
select(obj, 'selector.to.val'); // ['val to select']
select(obj, 'selector.to.val', 'selector.to'); // ['val to select', { val: 'val to select' }]
```