Skip to content

Instantly share code, notes, and snippets.

@LeeCheneler
Created December 4, 2020 13:45
Show Gist options
  • Save LeeCheneler/cc768d41b95daa2f191c80a7f6289f34 to your computer and use it in GitHub Desktop.
Save LeeCheneler/cc768d41b95daa2f191c80a7f6289f34 to your computer and use it in GitHub Desktop.
find key paths in object with numeric value
const findKeysWithNumericValue = (obj, keyPath = "") => {
const keys = Object.keys(obj)
const numericKeys = []
const preKey = keyPath.length > 0 ? `${keyPath}.` : ""
for (let k of keys) {
const value = obj[k]
if (typeof value === "number") {
numericKeys.push(`${preKey}${k}`)
} else if (typeof value === "object") {
numericKeys.push(...findKeysWithNumericValue(value, `${preKey}${k}`))
}
}
return numericKeys
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment