简体   繁体   中英

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

I am making a json array after watching a json array .I am facing one problem it create array inside the object .but I need to add array inside the parent array I am getting this result

[
  {
    "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": []
  }
]

But I need to get this out put

[
  {
    "a": {
      "commandList": [

      ],
      "testCaseList": [

      ]
    },

  },
  {
    "b": {
      "commandList": [

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

          ],
          "testCaseList": [

          ]
        },

      },
      {
        "b-b-2": {
          "commandList": [

          ],
          "testCaseList": [

          ]
        },

      }
    ]
    },

  },
  {
    "c-1": {
      "commandList": [

      ],
      "testCaseList": [

      ]
    },

  }
]

I try to change this line

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

to change

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

Then I am getting error

Here is my fiddle http://jsfiddle.net/tJ7Kq/2/

In the line you tried to change, Use

item[inputItem.id].testCaseList

instead of

item.testCaseList

Updated Fiddle .

Try visualizing the data if it gets too messy. http://jsonblob.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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