繁体   English   中英

为什么在制作json对象时创建新数组?

[英]why it is creating array new array while making json object?

我在观看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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM