使用javascript

时间:2018-05-12 15:07:23

标签: javascript typescript

我有以下格式的JSON数据,需要根据特定值进行过滤:

[
{
    "id": 0,
    "name": "ROOT-0",
    "childs": [
        {
            "id": 1,
            "name": "ROOT-1",
            "childs": [
                {
                    "id": 11,
                    "name": "ROOT-11",
                },
                {
                    "id": 12,
                    "name": "ROOT-12",
                },
            ]
        },
        {
            "id": 2,
            "name": "ROOT-2",
            "childs": [
                {
                    "id": 21,
                    "name": "ROOT-21",
                },
                {
                    "id": 22,
                    "name": "ROOT-22",
                },
            ]
        },
        {
            "id": 3,
            "name": "ROOT-3",
            "childs": [
                {
                    "id": 31,
                    "name": "ROOT-31",
                },
                {
                    "id": 32,
                    "name": "ROOT-32",
                },

            ]
        }
    ]
}]

如果我寻找 ROOT-11 / ROOT-12 ,我需要将 ROOT-1 作为最终结果。

我尝试使用以下代码进行过滤

var res = data[0].filter(function f(o) { if (o.name.includes("ROOT-11")) return o; })

但我无法控制逻辑。有没有办法实现我想要的输出

2 个答案:

答案 0 :(得分:1)

您可以,对于任意计数的嵌套子项,通过迭代实际级别使用 recusion方法,如果没有找到,则检查具有实际名称的子项。

如果找到了想要的名字,则通过所有嵌套调用移交父母的姓名并返回。



function getParent(array, search, parent) {
    return array.some(o => o.name === search || o.children && (parent = getParent(o.children, search, o.name)))
        && parent;
}

var data = [{ id: 0, name: "ROOT-0", children: [{ id: 1, name: "ROOT-1", children: [{ id: 11, name: "ROOT-11" }, { id: 12, name: "ROOT-12" }] }, { id: 2, name: "ROOT-2", children: [{ id: 21, name: "ROOT-21" }, { id: 22, name: "ROOT-22" }] }, { id: 3, name: "ROOT-3", children: [{ id: 31, name: "ROOT-31" }, { id: 32, name: "ROOT-32" }] }] }]


console.log(getParent(data, 'ROOT-0'));  // undefined no parent found
console.log(getParent(data, 'ROOT-1'));  // ROOT-0
console.log(getParent(data, 'ROOT-11')); // ROOT-1
console.log(getParent(data, 'ROOT-31')); // ROOT-3

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




答案 1 :(得分:1)

您可以使用一些过滤器和查找来获取项目,以获得您要查找的结果:



let items = [{
  "id": 0,
  "name": "ROOT-0",
  "childs": [{
      "id": 1,
      "name": "ROOT-1",
      "childs": [{
          "id": 11,
          "name": "ROOT-11",
        },
        {
          "id": 12,
          "name": "ROOT-12",
        },
      ]
    },
    {
      "id": 2,
      "name": "ROOT-2",
      "childs": [{
          "id": 21,
          "name": "ROOT-21",
        },
        {
          "id": 22,
          "name": "ROOT-22",
        },
      ]
    },
    {
      "id": 3,
      "name": "ROOT-3",
      "childs": [{
          "id": 31,
          "name": "ROOT-31",
        },
        {
          "id": 32,
          "name": "ROOT-32",
        },

      ]
    }
  ]
}]

function find(name) {
  let result
  items.filter(item =>
    result = item.childs.find(item2 =>
      item2.childs.filter(i => i.name == name).length > 0
    )
  )
  return result.name || ''
}

console.log(find('ROOT-11'))
console.log(find('ROOT-22'))
console.log(find('ROOT-32'))