Home > Mobile >  How to get the rest of a value of object inside of array
How to get the rest of a value of object inside of array

Time:10-23

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);
  • Related