Created
December 4, 2020 13:45
-
-
Save LeeCheneler/cc768d41b95daa2f191c80a7f6289f34 to your computer and use it in GitHub Desktop.
find key paths in object with numeric value
This file contains 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 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