有没有办法快速删除或清空json对象数组?

时间:2018-03-25 00:11:16

标签: javascript json angular

我有一个像这样的json数组

this.nodes = [
  {
    "id": 1,
    "parent": null,
    "name": "",
    "children": [
      {
        "id": 2,
        "parent": 1,
        "name": "ok it works",
        "children": []
      },
      {
        "id": 4,
        "parent": 1,
        "name": "ok it works",
        "children": []
      }
    ]
  },
  {
    "id": 3,
    "parent": null,
    "name": "",
    "children": [
      {
        "id": 5,
        "parent": 3,
        "name": "ok it works",
        "children": []
      },
      {
        "id": 6,
        "parent": 3,
        "name": "",
        "children": []
      }
    ]
  }
]

清空数组的最快方法是什么,我已经尝试了

this.nodes = [] 

但在运行该行后出现此错误

vendor.f0f5c3b4d9bf656cfa2b.js:8 ERROR SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at Response.Body.json (vendor.f0f5c3b4d9bf656cfa2b.js:641)
    at MapSubscriber.project (app.f0f5c3b4d9bf656cfa2b.js:171)
    at MapSubscriber._next (vendor.f0f5c3b4d9bf656cfa2b.js:676)
    at MapSubscriber.Subscriber.next (vendor.f0f5c3b4d9bf656cfa2b.js:420)
    at XMLHttpRequest.a (vendor.f0f5c3b4d9bf656cfa2b.js:655)
    at ZoneDelegate.invokeTask (polyfills.f0f5c3b4d9bf656cfa2b.js:36)
    at Object.onInvokeTask (vendor.f0f5c3b4d9bf656cfa2b.js:36)
    at ZoneDelegate.invokeTask (polyfills.f0f5c3b4d9bf656cfa2b.js:36)
    at Zone.runTask (polyfills.f0f5c3b4d9bf656cfa2b.js:36)

我有一个深度嵌套的json对象数组,因此有一种快速删除它的方法,将其重置为一个空数组,而不必递归遍历整个树并删除每个子项,然后再删除父母一直到主要的父母?

顺便说一下,我在Angularjs中运行它,并根据我在上面显示的数组渲染一组嵌套组件。

1 个答案:

答案 0 :(得分:0)

将其存储到变量中,因此:

{{1}}