3 回答

TA贡献1911条经验 获得超7个赞
更新
我想你可以为此使用 reduce,我现在没有电脑来测试它,但它会是这样的
const removeType = (node, type) => {
if (node === null) {
return null;
} else {
return node.reduce((acc, child) => {
if(child["type"] === type) {
const removedChild = removeType(child["children"], type);
acc = [...acc, ...removedChild];
} else {
child.children = removeType(child["children"], type);
acc.push(child);
}
return acc;
}, []);
}
}
第二次更新
代码减少:
const removeType = (node, type) => {
if (!node) return;
return node.reduce((acc, child) => {
if(child["type"] === type) {
const removedChild = removeType(child["children"], type);
acc = [...acc, ...removedChild];
} else {
child.children = removeType(child["children"], type);
acc.push(child);
}
return acc;
}, []);
}

TA贡献1887条经验 获得超5个赞
我认为这个答案与提供的其他答案完全不同,可以将其添加为替代方案。它具有与 Thankyou 的答案相同的递归结构,但它简化了假设,即您的输入始终是一个数组,所有非零children节点也是如此。
const removeType = (node, target) =>
node .flatMap (({type, children, ...rest}) =>
type === target
? children ? removeType (children, target) : []
: [{...rest, type, children: children && (removeType (children, target))}]
)
const sampleData = [{name: "parent", type: "a", children: [{name: "childA", type: "a", children: null},{name: "childB", type: "b", children: [{name: "grandChildA", type: "a", children: null},{name: "grandChildB", type: "a", children: null}]}, {name: "childC", type: "a", children: null}]}]
console .log (
removeType (sampleData, 'b')
)
.as-console-wrapper {min-height: 100% !important; top: 0}

TA贡献1858条经验 获得超8个赞
Array.prototype.flatMap
这是使用、一些数学归纳法和一些相互递归来简化程序的方法-
removeType
接受一个数组nodes
和一个要删除的查询类型,q
removeType1
接受单个node
和要删除的查询类型,q
const removeType = (nodes, q) =>
(nodes || []).flatMap(n => removeType1(n, q)) // <-- removeType1
const removeType1 = (node, q) =>
q === node.type
? removeType(node.children) // <-- removeType
: { ...node, children: removeType(node.children, q) } // <-- removeType
const input =
[{name:"parent",type:"a",children:[{name:"childA",type:"a",children:null},{name:"childB",type:"b",children:[{name:"grandChildA",type:"a",children:null},{name:"grandChildB",type:"a",children:null}]},{name:"childC",type:"a",children:null}]}]
const result =
removeType(input, "b")
console.log(result)
输出 -
[
{
"name": "parent",
"type": "a",
"children": [
{
"name": "childA",
"type": "a",
"children": []
},
{
"name": "grandChildA",
"type": "a",
"children": []
},
{
"name": "grandChildB",
"type": "a",
"children": []
},
{
"name": "childC",
"type": "a",
"children": []
}
]
}
]
请注意,这result是一个新对象,input不会因调用.removeType
相互递归非常适合解决上述问题,但如果您真的只想要一个函数怎么办?
const removeType = (t, q) =>
Array.isArray(t) // <-- array
? t.flatMap(node => removeType(node, q))
: Object(t) === t // <-- object (node)
? q === t.type
? removeType(t.children, q)
: { ...t, children: removeType(t.children, q) }
: t // <-- else (no operation)
const input =
[{name:"parent",type:"a",children:[{name:"childA",type:"a",children:null},{name:"childB",type:"b",children:[{name:"grandChildA",type:"a",children:null},{name:"grandChildB",type:"a",children:null}]},{name:"childC",type:"a",children:null}]}]
const result =
removeType(input, "b")
console.log(result)
输出大部分是相同的,除了注意这个是如何保存的children: null。我们的原始程序产生更正确的children: []-
[
{
"name": "parent",
"type": "a",
"children": [
{
"name": "childA",
"type": "a",
"children": null
},
{
"name": "grandChildA",
"type": "a",
"children": null
},
{
"name": "grandChildB",
"type": "a",
"children": null
},
{
"name": "childC",
"type": "a",
"children": null
}
]
}
]
添加回答
举报