【问题标题】:How to delete Json Object in large Json Array in Angular JS如何在 Angular JS 中删除大型 Json 数组中的 Json 对象
【发布时间】:2018-02-25 01:40:58
【问题描述】:

目前我有非常大的 JSON 数据,我想在使用 JSON 数据在 Angular JS 控制器中处理之前制作修剪版本。

在下面的 JSON 数据中,我不想有 commenthtmlComment 元素,如何在处理数据之前删除它们并拥有新的非常轻量级的 JSON 数据

为了简单起见,我制作了非常简单的 JSON 数组,但实际上我有非常多的数据,几乎 100mb。

我已经解决了很多问题仍然无法提出

  1. Ref 1
  2. Ref 2
  3. Ref 3

以下是 JSON 数据

[
  {
    "executions": [
      {
        "id": 241049,
        "orderId": 212250,
        "executionStatus": "1",
        "executedOn": "19/Jul/17 7:42 PM",
        "executedBy": "ext1",
        "executedByDisplay": "Person1",
        "comment": "Comment1",
        "htmlComment": "HTML1"
      },
      {
        "id": 241049,
        "orderId": 212250,
        "executionStatus": "1",
        "executedOn": "19/Jul/17 7:42 PM",
        "executedBy": "ext1",
        "executedByDisplay": "Person1",
        "comment": "Comment1",
        "htmlComment": "HTML1"
      },
      {
        "id": 241049,
        "orderId": 212250,
        "executionStatus": "1",
        "executedOn": "19/Jul/17 7:42 PM",
        "executedBy": "ext1",
        "executedByDisplay": "Person1",
        "comment": "Comment1",
        "htmlComment": "HTML1"
      }
    ],
    "currentlySelectedExecutionId": "",
    "recordsCount": 210,
    "stepDefectCount": 0,
    "totalDefectCount": 0
  },
  {
    "executions": [
      {
        "id": 241049,
        "orderId": 212250,
        "executionStatus": "1",
        "executedOn": "19/Jul/17 7:42 PM",
        "executedBy": "ext1",
        "executedByDisplay": "Person1",
        "comment": "Comment1",
        "htmlComment": "HTML1"
      },
      {
        "id": 241049,
        "orderId": 212250,
        "executionStatus": "1",
        "executedOn": "19/Jul/17 7:42 PM",
        "executedBy": "ext1",
        "executedByDisplay": "Person1",
        "comment": "Comment1",
        "htmlComment": "HTML1"
      },
      {
        "id": 241049,
        "orderId": 212250,
        "executionStatus": "1",
        "executedOn": "19/Jul/17 7:42 PM",
        "executedBy": "ext1",
        "executedByDisplay": "Person1",
        "comment": "Comment1",
        "htmlComment": "HTML1"
      }
    ],
    "currentlySelectedExecutionId": "",
    "recordsCount": 210,
    "stepDefectCount": 0,
    "totalDefectCount": 0
  },
  {
    "executions": [
      {
        "id": 241049,
        "orderId": 212250,
        "executionStatus": "1",
        "executedOn": "19/Jul/17 7:42 PM",
        "executedBy": "ext1",
        "executedByDisplay": "Person1",
        "comment": "Comment1",
        "htmlComment": "HTML1"
      },
      {
        "id": 241049,
        "orderId": 212250,
        "executionStatus": "1",
        "executedOn": "19/Jul/17 7:42 PM",
        "executedBy": "ext1",
        "executedByDisplay": "Person1",
        "comment": "Comment1",
        "htmlComment": "HTML1"
      },
      {
        "id": 241049,
        "orderId": 212250,
        "executionStatus": "1",
        "executedOn": "19/Jul/17 7:42 PM",
        "executedBy": "ext1",
        "executedByDisplay": "Person1",
        "comment": "Comment1",
        "htmlComment": "HTML1"
      }
    ],
    "currentlySelectedExecutionId": "",
    "recordsCount": 210,
    "stepDefectCount": 0,
    "totalDefectCount": 0
  }
]

【问题讨论】:

    标签: angularjs arrays json slice


    【解决方案1】:

    在每个数据数组上使用地图:

    var lightData = rawData.map(function(item) { 
      // use Object.assign to prevent mutating original object
      var newItem = Object.assign({}, item);
      var lightExecutions = item.executions.map(function(d) {
        var ld = {
          id: d.id,
          orderId: d.orderId,
          executionStatus: d.executionStatus,
          executedOn: d.executedOn,
          executedBy: d.executedBy,
          executedByDisplay: d.executedByDisplay,
        };
        return ld;
      });
      newItem.executions = lightExecutions;
      return newItem;
    });
    

    只有您包含在映射对象中的字段才会填充新数据集。

    【讨论】:

    • 添加了Object.assign以防止原始数据发生突变。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 2022-06-28
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多