-1

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

  • 1
    `obj.restaurant.categories.map(e => e.name)` – Spectric Oct 23 '22 at 04:02
  • Does this answer your question? [From an array of objects, extract value of a property as array](https://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array) – pilchard Oct 23 '22 at 11:00

2 Answers2

1

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)
flyingfox
  • 13,414
  • 3
  • 24
  • 39
0
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);