Skip to content

Instantly share code, notes, and snippets.

@LasaleFamine
Created July 14, 2020 15:25
Show Gist options
  • Select an option

  • Save LasaleFamine/5a806d21f472e6b0cb7136c6d3e96be9 to your computer and use it in GitHub Desktop.

Select an option

Save LasaleFamine/5a806d21f472e6b0cb7136c6d3e96be9 to your computer and use it in GitHub Desktop.
Find a couple of `key` - `value` within an object. Recursive.
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