Prepare repository for merge
This commit is contained in:
35
javascript/snippets/deep-get.md
Normal file
35
javascript/snippets/deep-get.md
Normal file
@ -0,0 +1,35 @@
|
||||
---
|
||||
title: Get nested value in object based on array of keys
|
||||
type: snippet
|
||||
tags: [object]
|
||||
cover: mask-quiet
|
||||
dateModified: 2020-10-19T18:51:03+03:00
|
||||
---
|
||||
|
||||
Gets the target value in a nested JSON object, based on the `keys` array.
|
||||
|
||||
- Compare the keys you want in the nested JSON object as an `Array`.
|
||||
- Use `Array.prototype.reduce()` to get the values in the nested JSON object one by one.
|
||||
- If the key exists in the object, return the target value, otherwise return `null`.
|
||||
|
||||
```js
|
||||
const deepGet = (obj, keys) =>
|
||||
keys.reduce(
|
||||
(xs, x) => (xs && xs[x] !== null && xs[x] !== undefined ? xs[x] : null),
|
||||
obj
|
||||
);
|
||||
```
|
||||
|
||||
```js
|
||||
let index = 2;
|
||||
const data = {
|
||||
foo: {
|
||||
foz: [1, 2, 3],
|
||||
bar: {
|
||||
baz: ['a', 'b', 'c']
|
||||
}
|
||||
}
|
||||
};
|
||||
deepGet(data, ['foo', 'foz', index]); // get 3
|
||||
deepGet(data, ['foo', 'bar', 'baz', 8, 'foz']); // null
|
||||
```
|
||||
Reference in New Issue
Block a user