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