说我有一个像下面这样的嵌套数组。现在我想删除元素而不是children。我想将其childs值设为parent array / children值。
const steps = [
{
step_id: 100,
type: 'TEXT',
title: null,
item_id: 1,
children: [
{
step_id: 102,
type: 'TEXT',
title: null,
item_id: 1,
children: [
{
step_id: 104,
type: 'TEXT',
title: null,
item_id: 1,
children: [
]
}
]
}
]
}
];
For Example I want to remove when step_id=102
and its other elements and make it's children value to it's parent children value.I want something like this result.
result= steps: [
{
step_id: 100,
type: 'TEXT',
title: null,
item_id: 1,
children: [
{
step_id: 104,
type: 'TEXT',
title: null,
item_id: 1,
children: [
]
}
]
}
]
我尝试下面的代码,但未返回正确的结果。请帮我。
谢谢。
const steps = [
{
step_id: 100,
type: 'TEXT',
title: null,
item_id: 1,
children: [
{
step_id: 102,
type: 'TEXT',
title: null,
item_id: 1,
children: [
{
step_id: 104,
type: 'TEXT',
title: null,
item_id: 1,
children: [
]
}
]
}
]
}
];
const removeItem ={step_id: 102, type: "TEXT", title: null, item_id: 1, children: Array(0)};
const removeElement = (steps, removeItem) => {
steps.forEach((step) => {
if (step.step_id == removeItem.step_id) {
steps = step.children;
} else {
removeElement(step.children, removeItem);
}
});
//console.log(steps);
return steps
};
console.log(removeElement(steps, removeItem))