I have an object that looks like this:
const object = {
head: 1,
eyes: 2,
arms: 2,
legs: 3
}
I want to loop over this object, and that is to print the name of each key, for example. eyesfor the sum of the value.
this will lead to:
head
eyes
eyes
arms
arms
legs
legs
legs
I currently have this solution, but it seems that it can be done more accurately and easily.
Object.keys(object)
.map(key => {
return [...Array(object[key])].map( (_, i) => {
return console.log(key)
})
Any suggestions?
source
share