Last active
January 29, 2020 12:03
-
-
Save nippe/b30a9ccd6b5e846b245166be3bf1fa72 to your computer and use it in GitHub Desktop.
Lodash Native Helpers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const flattenDeep = (arr) => Array.isArray(arr) | |
| ? arr.reduce( (a, b) => a.concat(flattenDeep(b)) , []) | |
| : [arr] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const get = (obj, path, defaultValue) => { | |
| const travel = regexp => | |
| String.prototype.split | |
| .call(path, regexp) | |
| .filter(Boolean) | |
| .reduce((res, key) => (res !== null && res !== undefined ? res[key] : res), obj); | |
| const result = travel(/[,[\]]+?/) || travel(/[,[\].]+?/); | |
| return result === undefined || result === obj ? defaultValue : result; | |
| }; | |
| var object = { a: [{ b: { c: 3 } }] }; | |
| var result = get(object, 'a[0].b.c', 1); | |
| // output: 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const isEmpty = obj => [Object, Array].includes((obj || {}).constructor) && !Object.entries((obj || {})).length; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment