Update deepGet.md
This commit is contained in:
@ -9,18 +9,6 @@ Compare the keys you want in the nested JSON object as an `Array`.
|
|||||||
Use `Array.prototype.reduce()` to get value from nested JSON object one by one.
|
Use `Array.prototype.reduce()` to get value from nested JSON object one by one.
|
||||||
If the key exists in object, return target value, otherwise, return `null`.
|
If the key exists in object, return target value, otherwise, return `null`.
|
||||||
|
|
||||||
```js
|
|
||||||
---
|
|
||||||
title: deepGet
|
|
||||||
tags: object,intermediate
|
|
||||||
---
|
|
||||||
|
|
||||||
Returns 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 value from nested JSON object one by one.
|
|
||||||
If the key exists in object, return target value, otherwise, return `null`.
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const deepGet = (obj, keys) =>
|
const deepGet = (obj, keys) =>
|
||||||
keys.reduce(
|
keys.reduce(
|
||||||
@ -29,25 +17,6 @@ const deepGet = (obj, keys) =>
|
|||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
|
||||||
let index = 2;
|
|
||||||
const data = {
|
|
||||||
foo: {
|
|
||||||
foz: [1, 2, 3],
|
|
||||||
bar: {
|
|
||||||
baz: ['a', 'b', 'c']
|
|
||||||
}
|
|
||||||
},
|
|
||||||
name: null,
|
|
||||||
isDark: false
|
|
||||||
};
|
|
||||||
deepGet(data, ['foo', 'foz', index]); // get 3
|
|
||||||
deepGet(data, ['foo', 'bar', 'baz', 8, 'foz']); // undefined
|
|
||||||
deepGet(data, ['name']); // null
|
|
||||||
deepGet(data, ['isDark']); // false
|
|
||||||
```
|
|
||||||
```
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
let index = 2;
|
let index = 2;
|
||||||
const data = {
|
const data = {
|
||||||
|
|||||||
Reference in New Issue
Block a user