为什么它在创建json对象时创建数组新数组

why it is creating array new array while making json object?

本文关键字:创建 数组 新数组 对象 json 为什么      更新时间:2023-09-26

我在观看json数组后制作json数组。我面临一个问题,它在对象内创建数组,但我需要在父数组内添加数组我得到这个结果

[
  {
    "a": {
      "commandList": [],
      "testCaseList": []
    },
    "testCaseList": []
  },
  {
    "b": {
      "commandList": [],
      "testCaseList": []
    },
    "testCaseList": [
      {
        "b-a-1": {
          "commandList": [],
          "testCaseList": []
        },
        "testCaseList": []
      },
      {
        "b-b-2": {
          "commandList": [],
          "testCaseList": []
        },
        "testCaseList": []
      }
    ]
  },
  {
    "c-1": {
      "commandList": [],
      "testCaseList": []
    },
    "testCaseList": []
  }
]

但是我需要把这个拿出来

[
  {
    "a": {
      "commandList": [
      ],
      "testCaseList": [
      ]
    },
  },
  {
    "b": {
      "commandList": [
      ],
          "testCaseList": [
      {
        "b-a-1": {
          "commandList": [
          ],
          "testCaseList": [
          ]
        },
      },
      {
        "b-b-2": {
          "commandList": [
          ],
          "testCaseList": [
          ]
        },
      }
    ]
    },
  },
  {
    "c-1": {
      "commandList": [
      ],
      "testCaseList": [
      ]
    },
  }
]

我试着改变这行

 item.testCaseList = inputItem.children.map(mapItem);

改变

var obj= JSON.parse(sessionStorage.getItem(inputItem.id)
obj.testCaseList=inputItem.children.map(mapItem)

然后我得到错误

这是我的小提琴http://jsfiddle.net/tJ7Kq/2/

在您尝试更改的行中,使用

item[inputItem.id].testCaseList

代替

item.testCaseList

更新的小提琴。

如果数据太乱,尝试可视化。http://jsonblob.com/