if I have this:
{
"restaurant": {
"categories": [
{
"name": "Italia"
},
{
"name": "Modern"
}
],
}
}
I've tried to get the value with restaurant.categories, it return [object, object],[object, object]
expected result: Italia, Modern
CodePudding user response:
You can use map()
to do it
let data = {
"restaurant": {
"categories": [
{
"name": "Italia"
},
{
"name": "Modern"
}
],
}
}
let result = data.restaurant.categories.map(n => n.name)
console.log(result)
CodePudding user response:
const data = {
"restaurant": {
"categories": [
{
"name": "Italia"
},
{
"name": "Modern"
}
],
}
}
// data.restaurant.categories is array of object so you have to return value from it.
const ans = data.restaurant.categories.map((item)=> item.name);
//get desired output:
console.log(ans);