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

标签 javascript jquery

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

然后我收到错误

这是我的 fiddle http://jsfiddle.net/tJ7Kq/2/

最佳答案

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

item[inputItem.id].testCaseList

而不是

item.testCaseList

已更新Fiddle .

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

关于javascript - 为什么它在创建 json 对象时创建数组新数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23623816/

相关文章:

javascript - 购物车内容总值(value)的 WooCommerce 进度条

javascript - JavaScript(或 jQuery)中是否有 'has focus'?

javascript - 使用复选框的 jQuery 组合过滤器

javascript - 更改克隆中的某些内容

javascript - D3 可折叠力布局可变链接长度

javascript - 根据字符标记 JavaScript 字符串

javascript - Fullcalendar无法访问drop中的html5数据属性,仅在Firefox上

javascript - 使用千位分隔符格式的按键功能进行计算

javascript - 流体图像上的 Jquery JCrop 功能

javascript - MVC 和 Javascript/JQuery 验证库