【问题标题】:how to parse array of objects inside an object using maps or forEach如何使用映射或 forEach 解析对象内的对象数组
【发布时间】:2020-03-14 12:35:54
【问题描述】:

我的 JSON 数据如下所示:{ "objects": [ { "name": "case1", "description": "description of case1", "shapes": [ { "shape_name": "circle" } ], "order_ref": [ { "id": "2233" } ] }, { "name": "case2", "description": "description of case2", "shapes": [ { "shape_name": "heart" }, { "shape_name": "square" } ], "order_ref": [ { "id": "1212" } ] } ] }

我需要根据数据创建一个表。 该表应如下所示:

Id      Shape      Name    Description
2233   Circle      Case1   Desc of case1
1212   Heart       Case2   Desc of case2
1212   Square      Case2    Desc of case2

我尝试使用地图解析 JSON 数据:我能够获取名称和描述,但无法进入形状和 order_ref。你能建议我使用地图或 forEach 的解决方案吗

【问题讨论】:

  • 基本上你只需要做同样的事情,但下一级..也许发布你为获取名称和描述所做的代码
  • 到目前为止你做了什么?您在哪里需要帮助?

标签: javascript json foreach javascript-objects


【解决方案1】:

您可以在 forEach 中执行 forEach,例如:

let array = [
                {
                    name: "case1",
                    description: "description of case1",
                    shapes: [
                        {
                            shape_name: "circle"
                        }
                    ],
                    order_ref: [
                        {
                            id: "2233"
                        }
                    ]
                },
                {
                    name: "case2",
                    description: "description of case2",
                    shapes: [
                        {
                            shape_name: "heart"
                        },
                        {
                            shape_name: "square"
                        }
                    ],
                    order_ref: [
                        {
                            id: "1212"
                        }
                    ]
                }
            ];

let newArray = [];

array.forEach(obj => {
  obj.shapes.forEach(shape => {
    let newObj = Object.assign({}, obj);
    newObj.shape_name = shape.shape_name;
    delete newObj.shapes;
    newObj.id = obj.order_ref[0].id;
    delete newObj.order_ref;
    newArray.push(newObj);
  });
});

console.log(newArray);

得到 newArray 后,你可以像往常一样使用和迭代它

【讨论】:

    【解决方案2】:

    你需要两个forEach()来实现表。

    你可以试试下面的方法

    var data = {
      "objects": [
          {
              "name": "case1",
              "description": "description of case1",
              "shapes": [
                  {
                      "shape_name": "circle"
                  }
              ],
              "order_ref": [
                  {
                      "id": "2233"
                  }
              ]
          },
          {
              "name": "case2",
              "description": "description of case2",
              "shapes": [
                  {
                      "shape_name": "heart"
                  },
                  {
                      "shape_name": "square"
                  }
              ],
              "order_ref": [
                  {
                      "id": "1212"
                  }
              ]
          }
      ]
    }
     var tr = '';
    data.objects.forEach(o => {
      o.shapes.forEach(io => {
        tr += `<tr><td>${o.order_ref[0].id}</td><td>${io.shape_name}</td><td>${o.name}</td><td>${o.description}</td></tr>`;
      });
    });
    document.querySelector('#tableData tbody').insertAdjacentHTML('beforeend', tr);
    table, th, td {
      border: 1px solid black;
    }
    td:first-letter{
      text-transform: capitalize
    }
    <table id="tableData">
      <thead>
        <th>Id</th>
        <th>Shape</th>
        <th>Name</th>
        <th>Description</th>
      </thead>
      <tbody></tbody>
    </table>

    【讨论】:

      【解决方案3】:

      如果您需要为每个订单 ref id 和形状输入一个条目,您可以使用 forEach,如下所示:

      let jsonArray = {
          "objects": [
              {
                  "name": "case1",
                  "description": "description of case1",
                  "shapes": [
                      {
                          "shape_name": "circle"
                      }
                  ],
                  "order_ref": [
                      {
                          "id": "2233"
                      }
                  ]
              },
              {
                  "name": "case2",
                  "description": "description of case2",
                  "shapes": [
                      {
                          "shape_name": "heart"
                      },
                      {
                          "shape_name": "square"
                      }
                  ],
                  "order_ref": [
                      {
                          "id": "1212"
                      }
                  ]
              }
          ]
      }
      
      let output = [];
      jsonArray.objects.forEach(object => {
          object.order_ref.forEach(order => {
              object.shapes.forEach(shape => {
                  output.push({
                      id: order.id,
                      shape: shape.shape_name,
                      name: object.name,
                      description: object.description
                  })
              })
          })
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-19
        • 2022-11-24
        • 2014-03-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多