首页 > 解决方案 > 使用 map 从包含项目数组的对象中获取项目

问题描述

我有一个看起来像这样的对象:

const items = {
    "data": {
      "app4": [
        {
          "id": 36,
          "name": "app1.acme.com",
          "port": 9447,
          "title": "Application Demo 1",
          "subtitle": "bootstapping demo 1"
        }
      ],
      "app5": [
        {
          "id": 37,
          "name": "app2.acme.com",
          "port": 9448,
          "title": "Application Demo 2",
          "subtitle": "bootstapping demo 2"
        }
      ],
      "app6": [
        {
          "id": 38,
          "name": "app3.acme.com",
          "port": 9449,
          "title": "Application Demo 3",
          "subtitle": "bootstapping demo 3"
        }
      ]
    }
  }

我正在尝试像这样迭代对象:

Object.entries(test.data).map(item => {
    item.map(function (item) {
        console.log(item[0].id)
        console.log(item[0].name)
      });
  }) 

但是,虽然我可以获得 ID 和名称,但它会undefined为每个地图回调重新调整。例如

undefined
undefined
36
undefined
undefined
undefined
37
undefined
undefined
undefined
38
undefined

标签: javascriptdictionary

解决方案


推荐阅读