javascript - 如何从树中删除每个对象

标签 javascript arrays treeview

我有 JSON 对象...并且想要从每个 child 直到最后一个 child 中删除名称键值,即(name-11,name-22)...使用 java 脚本函数...

{
  "id": "1",
  "name": "11",
  "child": [
{
  "id": 2,
  "name": "22",
  "child": [
    {
      "id": 3,
      "name": "33",
      "child": [
        {
          "id": 4,
          "name": "44",
          "child": [
            {
              "id": 5,
              "name": "55",
              "child": [
                {
                  "id": 6,
                  "name": "66"
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}
 ]
}

有什么正确的方法可以做到这一点吗?

最佳答案

可能是这样的......

let myJson = {
  "id": "1",
  "name": "11",
  "child": [{
    "id": 2,
    "name": "22",
    "child": [{
      "id": 3,
      "name": "33",
      "child": [{
        "id": 4,
        "name": "44",
        "child": [{
          "id": 5,
          "name": "55",
          "child": [{
            "id": 6,
            "name": "66"
          }]
        }]
      }]
    }]
  }]
};
let currentObject = myJson;
while (currentObject.child && currentObject.child[0]) {
  delete currentObject.name;
  currentObject = currentObject.child[0];
}
console.log(myJson)

关于javascript - 如何从树中删除每个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52117717/

相关文章:

java - 处理 Java Socket 中的一个或多个单词 .readLine()

javascript - 按对象属性过滤对象数组在 ReactJS 中不起作用

c# - 找到 TreeViewItem 来放置数据

python - 如何使用 Tkinter 将垂直滚动条附加到 TreeView ?

javascript - W3C 将 javascript 中的 html 标记显示为错误

javascript - 在内联脚本之后如何正确注册 yii2 项目中的 js 代码?

javascript - 跨浏览器的自定义协议(protocol)检测

ios - 如何解析 NSArray 以仅获取每个数组条目的一部分?

python - tkinter:如何序列化 TreeView ?

javascript - 添加到 DOM 后触发过渡动画