Created
July 14, 2020 15:25
-
-
Save LasaleFamine/5a806d21f472e6b0cb7136c6d3e96be9 to your computer and use it in GitHub Desktop.
Find a couple of `key` - `value` within an object. Recursive.
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 findFlatByKey = (choosenKey, choosenValue) => (ogg) => { | |
| let found = false; | |
| const step = (object, prev, currentDepth) => { | |
| currentDepth = currentDepth || 1 | |
| Object.keys(object).forEach((key) => { | |
| const value = object[key] | |
| const isarray = Array.isArray(value) | |
| const type = Object.prototype.toString.call(value) | |
| const isobject = ( | |
| type === '[object Object]' || | |
| type === '[object Array]' | |
| ) | |
| const newKey = prev | |
| ? `${prev}.${key}` | |
| : key | |
| if (object[choosenKey] === choosenValue) { | |
| found = true; | |
| return; | |
| } | |
| if (!isarray && isobject && Object.keys(value).length) { | |
| return step(value, newKey, currentDepth + 1) | |
| } | |
| }) | |
| } | |
| step(ogg); | |
| return found; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment