Answers for "get Nested Property Key By Value Comparasion"

0

get Nested Property Key By Value Comparasion

// Recursive function to get the name of property with given value from given object
const getNestedPropKeyByValueComparasion = (obj, value) => {
  // Check if it's an object
  if (!!obj && typeof obj === 'object') {
    let keys = Object.keys(obj)
    for (let key of keys ) {
      if (obj[key] === value)
        return key
      else {
        let resultInNested = getNestedPropKeyByValueComparasion(obj[key], value)
        if ( !!resultInNested ) 
          return resultInNested
      }
    }
  }
  return null
}
  // Alternatively if you don't want to check deeply nested values, simply remove the else block
Posted by: Guest on March-16-2021

Code answers related to "get Nested Property Key By Value Comparasion"

Code answers related to "Javascript"

Browse Popular Code Answers by Language