30
snippets/dig.md
Normal file
30
snippets/dig.md
Normal file
@ -0,0 +1,30 @@
|
||||
### dig
|
||||
|
||||
Returns the target value in a nested JSON object, based on the given key.
|
||||
|
||||
Use the `in` operator to check if `target` exists in `obj`.
|
||||
If found, return the value of `obj[target]`, otherwise use `Object.values(obj)` and `Array.reduce()` to recursively call `dig` on each nested object until the first matching key/value pair is found.
|
||||
|
||||
```
|
||||
const dig = (obj, target) =>
|
||||
target in obj
|
||||
? obj[target]
|
||||
: Object
|
||||
.values(obj)
|
||||
.reduce((acc, val) => {
|
||||
if (acc !== undefined) return acc;
|
||||
if (typeof val === 'object') return dig(val, target);
|
||||
}, undefined);
|
||||
```
|
||||
|
||||
```
|
||||
const data = {
|
||||
level1:{
|
||||
level2:{
|
||||
level3: 'some data'
|
||||
}
|
||||
}
|
||||
};
|
||||
dig(data, 'level3'); // 'some data'
|
||||
dig(data, 'level4'); // undefined
|
||||
```
|
||||
@ -55,6 +55,7 @@ detectDeviceType:browser
|
||||
difference:array,math
|
||||
differenceBy:array,function
|
||||
differenceWith:array,function
|
||||
dig:object,recursion
|
||||
digitize:math,array
|
||||
distance:math
|
||||
drop:array
|
||||
|
||||
15
test/dig/dig.js
Normal file
15
test/dig/dig.js
Normal file
@ -0,0 +1,15 @@
|
||||
const dig = (obj, target) =>
|
||||
target in obj
|
||||
? obj[target]
|
||||
: Object
|
||||
.values(obj)
|
||||
.reduce((acc, val) => {
|
||||
if (acc !== undefined) {
|
||||
return acc;
|
||||
}
|
||||
if (typeof val === "object") {
|
||||
return dig(val, target);
|
||||
}
|
||||
}, undefined);
|
||||
|
||||
module.exports = dig;
|
||||
32
test/dig/dig.test.js
Normal file
32
test/dig/dig.test.js
Normal file
@ -0,0 +1,32 @@
|
||||
const expect = require("expect");
|
||||
const dig = require("./dig.js");
|
||||
|
||||
const data = {
|
||||
level1:{
|
||||
level2:{
|
||||
level3: "some data",
|
||||
level3f: false,
|
||||
level3a: [1,2,3,4]
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
test("dig is a Function", () => {
|
||||
expect(dig).toBeInstanceOf(Function);
|
||||
});
|
||||
|
||||
test("Dig target success", () => {
|
||||
expect(dig(data, "level3")).toEqual("some data");
|
||||
});
|
||||
|
||||
test("Dig target with falsey value", () => {
|
||||
expect(dig(data, "level3f")).toEqual(false);
|
||||
});
|
||||
|
||||
test("Dig target with array", () => {
|
||||
expect(dig(data, "level3a")).toEqual([1,2,3,4]);
|
||||
});
|
||||
|
||||
test("Unknown target return undefined", () => {
|
||||
expect(dig(data, "level4")).toEqual(undefined);
|
||||
});
|
||||
Reference in New Issue
Block a user