Skip to content

Instantly share code, notes, and snippets.

@vikneshwar
Last active July 5, 2021 21:57
Show Gist options
  • Select an option

  • Save vikneshwar/b4066a9f44bf3a29df9ab0ee7f4b66c7 to your computer and use it in GitHub Desktop.

Select an option

Save vikneshwar/b4066a9f44bf3a29df9ab0ee7f4b66c7 to your computer and use it in GitHub Desktop.
let val = {
a: {b:{c:10}, d:{f:20}},
j: {l:{c:30}},
k:{k:{c:40, k:44}, h:{f:10}}
}
// expected output arr = [20,10] // all the inner most f
let arr = []
function getKeyValues(obj) {
for ( key in obj) {
console.log(typeof key)
if(typeof obj[key] === 'object') {
getKeyValues(obj[key])
} else if(typeof obj[key] !== 'object') {
arr.push(obj[key])
}
}
}
//[10, 20, 30, "k"]
console.log(arr)
arr = []
// to get the f value
function getSpecificValues(obj) {
for ( key in obj) {
if(typeof obj[key] === 'object') {
if(obj[key].hasOwnProperty('f')) {
console.log(obj[key].f)
} else {
getSpecificValues(obj[key])
}
}
}
}
getKeyValues(val)
console.log(arr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment