Created
March 10, 2020 11:24
-
-
Save Lelith/e910629d9454fccc7118bc5e880a0f58 to your computer and use it in GitHub Desktop.
calculate object entries with recursion
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
function calcObjectLength(myObject, size) { | |
Object.entries(myObject).forEach(([, value]) => { | |
if (!isObject(value)) { | |
console.log('+1'); | |
size += 1; | |
} else { | |
console.log('is object'); | |
size += calcObjectLength(value, size); | |
} | |
}); | |
return size; | |
} | |
try { | |
const myObject = { | |
fruit: { | |
banana: 1, | |
strawberry: 4, | |
}, | |
}; | |
const objectLength = calcObjectLength(myObject, 0); | |
console.log(objectLength); | |
} catch (e) { | |
console.log('error'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment