【问题标题】:How to get the rest of a value of object inside of array如何获取数组内对象的其余值
【发布时间】:2022-10-23 12:02:33
【问题描述】:

如果我有这个:

{
  "restaurant": {
    "categories": [
      {
        "name": "Italia"
      },
      {
        "name": "Modern"
      }
    ],
  }
}

我试图用餐厅.categories, 它返回 [object, object],[object, object]

预期结果:意大利,现代

【问题讨论】:

  • obj.restaurant.categories.map(e => e.name)
  • 完全正确:Array.map() 就是答案。请务必“点赞”Spectric 的评论,以及“点赞”、“接受”和“点赞” lucomt 的回复。

标签: javascript arrays json object


【解决方案1】:

你可以使用map() 来做

let data = {
  "restaurant": {
    "categories": [
      {
        "name": "Italia"
      },
      {
        "name": "Modern"
      }
    ],
  }
}

let result = data.restaurant.categories.map(n => n.name)

console.log(result)

【讨论】:

  • 有效,谢谢解答
猜你喜欢
  • 1970-01-01
  • 2021-06-04
  • 2015-11-20
  • 2023-04-04
  • 2023-01-05
  • 2019-09-19
  • 1970-01-01
  • 2019-10-17
  • 2016-06-03
相关资源
最近更新 更多