WIP: npm run tester failed

This commit is contained in:
Henry Szeto
2018-07-08 02:06:00 -07:00
parent daee302666
commit c1b433d061
3 changed files with 2005 additions and 1938 deletions

View File

@ -1,14 +1,56 @@
### functionName
### dig
Explain briefly what the snippet does.
Return the value in a nested JSON object by the key.
Explain briefly how the snippet works.
Given the key name (or target), it will look up the key in the object recursively and return the first value if found.
```js
const functionName = arguments =>
{functionBody}
```
const dig = (obj, target) => {
if (!obj) return;
if (obj[target]) return obj[target];
else {
return( Object.keys(obj).map(key => {
if (typeof(obj[key]) === "object") {
return dig(obj[key], target);
}
}).filter(item => item !== undefined)[0] );
}
};
```
```js
functionName('sampleInput'); // 'sampleOutput'
```
const data = {
name: "John Doe",
details: {
phone: "9876543210",
email: "john@example.com",
address: {
street: "123 ABC St.",
state: "CA",
zip: "98765"
},
extra1: {
nickName: "Johnny"
},
extra2: {
nickName: "JD"
},
hobby: "coding",
snacks: ["chips", 'candy', 'coke']
},
l1:{
l2:{
l3:{
l4: "4 levels deep."
}
}
}
};
const hobby = dig(data, 'hobby'); // "coding"
const name = dig(data, 'nickName'); // "Johnny"
const food = dig(data, 'snacks'); // ["chips","candy","coke"]
const level4= dig(data, 'l4'); // "4 levels deep."
const nada = dig(data, 'blahblah') // undefined
```

View File

@ -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

File diff suppressed because it is too large Load Diff