3 回答

TA貢獻(xiàn)1911條經(jīng)驗 獲得超7個贊
更新
我想你可以為此使用 reduce,我現(xiàn)在沒有電腦來測試它,但它會是這樣的
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貢獻(xiàn)1887條經(jīng)驗 獲得超5個贊
我認(rèn)為這個答案與提供的其他答案完全不同,可以將其添加為替代方案。它具有與 Thankyou 的答案相同的遞歸結(jié)構(gòu),但它簡化了假設(shè),即您的輸入始終是一個數(shù)組,所有非零children節(jié)點也是如此。
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貢獻(xiàn)1858條經(jīng)驗 獲得超8個贊
Array.prototype.flatMap
這是使用、一些數(shù)學(xué)歸納法和一些相互遞歸來簡化程序的方法-
removeType
接受一個數(shù)組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不會因調(diào)用.removeType
相互遞歸非常適合解決上述問題,但如果您真的只想要一個函數(shù)怎么辦?
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。我們的原始程序產(chǎn)生更正確的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
}
]
}
]
添加回答
舉報