在JSON数组中查找值并从父节点中删除找到的节点

时间:2018-08-19 17:51:32

标签: javascript

var trees = [
  {
    name: "alex",
    lastname: "watson",
    city: "California",
    state: "Alameda",
    childrens: [
      { name: "shane watson", city: "Troy", state: "Alabama" },
      {
        name: "adam watson",
        city: "Palmer",
        state: "Alaska",
        childrens: [
          { name: "ana watson", city: "Avondale", state: "Arizona" },
          { name: "ama watson", city: "Douglas", state: "Arizona" }
        ]
      }
    ]
  },
  {
    name: "adam",
    lastname: "ronaldo",
    city: "Bradenton",
    state: "Florida",
    childrens: [
      { name: "austin ronaldo", city: "Alhambra", state: "California" },
      {
        name: "kira ronaldo",
        city: "Calexico",
        state: "California",
        childrens: [
          { name: "sam ronaldo", city: "Chico", state: "California" },
          {
            name: "godwin ronaldo",
            city: "Eureka",
            state: "California",
            childrens: [
              { name: "michael ronaldo", city: "Buffalo", state: "New York" }
            ]
          }
        ]
      }
    ]
  }
];
  • 在JSON数组变量树中查找城市(例如city =“ Buffalo”或name =“ Godwin ronaldo”)
  • 从父树变量中删除找到的节点。

this function给出匹配的节点:

function findMatchingNodes(nodes, predicate) {
  const results = [];
  function walk(node) {
    if (predicate(node)) {
      results.push(node);
    }
    (node.childrens || []).forEach(walk);
  }
  nodes.forEach(walk);
  return results;
}

函数调用

const matches = findMatchingNode(
  trees,
  (n) => n.city === 'Buffalo' || n.name === 'godwin ronaldo'
);

我要删除匹配的节点。

matches.forEach(node => {
  delete node;
});

输出:SyntaxError:在严格模式下删除局部变量

https://jsfiddle.net/5sve3nxc/

正在用小提琴推动树木的子节点。我想删除节点而不是推送。

1 个答案:

答案 0 :(得分:0)

虽然需要拼接数组,但如果谓词为true,则需要从数组末尾进行迭代以维护索引。

function deleteFromArray(array, predicate) {
    var i = array.length;

    while (i--) {
        if (predicate(array[i])) {
            array.splice(i, 1);
            continue;
        }
        if (array[i].children) {
            deleteFromArray(array[i].children, predicate);
        }
    }
}

var tree = [{ name: "alex", lastname: "watson", city: "California", state: "Alameda", children: [{ name: "shane watson", city: "Troy", state: "Alabama" }, { name: "adam watson", city: "Palmer", state: "Alaska", children: [{ name: "ana watson", city: "Avondale", state: "Arizona" }, { name: "ama watson", city: "Douglas", state: "Arizona" }] }] }, { name: "adam", lastname: "ronaldo", city: "Bradenton", state: "Florida", children: [{ name: "austin ronaldo", city: "Alhambra", state: "California" }, { name: "kira ronaldo", city: "Calexico", state: "California", children: [{ name: "sam ronaldo", city: "Chico", state: "California" }, { name: "godwin ronaldo", city: "Eureka", state: "California", children: [{ name: "michael ronaldo", city: "Buffalo", state: "New York" }] }] }] }];

deleteFromArray(tree, ({ city, name }) => city === 'Buffalo' || name === 'godwin ronaldo');

console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }