Currently I was wondering if it was possible and how to use object entries and map to get my desired result.
Currently, my data is:
let data = {
moreData: {one: 'one', two: '2', three: 'three'},
city: 'New York',
favFood: 'Steak',
favSite: 'StackOverflow',
};
And my code is:
Object.entries(data).map(([key,value]) => value
? console.log(`this is the ${key} and the value is: ${value}`)
: ''
)
This will output:
"this is the moreData and the value is: [object Object]"
"this is the city and the value is: New York"
"this is the favFood and the value is: Steak"
"this is the favSite and the value is: StackOverflow"
However for the moreData object, I would like it to only pick the two: 'two', but I have no clue how to do this...
Is there anyone who could help me with this?
My desired result would be:
"this is the moreData and the value is: 2"
"this is the city and the value is: New York"
"this is the favFood and the value is: Steak"
"this is the favSite and the value is: StackOverflow"
Thank you in advance.
CodePudding user response:
One way to do that would be setting a fallback when value.two
does not exist (equals null | undefined
).
let data = {
moreData: {one: 'one', two: '2', three: 'three'},
city: 'New York',
favFood: 'Steak',
favSite: 'StackOverflow',
};
Object.entries(data).map(([key,value]) => value
? console.log(`this is the ${key} and the value is: ${value.two ?? value}`)
: ''
)