//原数组
const data = ref([
{
key: 1,
name: "John Brown sr.",
age: 60,
address: "New York No. 1 Lake Park",
index:1,
children: [
{
key: 11,
name: "John Brown",
age: 42,
address: "New York No. 2 Lake Park",
index:11,
},
{
key: 12,
name: "John Brown jr.",
age: 30,
address: "New York No. 3 Lake Park",
index:12,
children: [
{
key: 121,
name: "Jimmy Brown",
age: 16,
address: "New York No. 3 Lake Park",
index:121,
},
],
},
{
key: 13,
name: "Jim Green sr.",
age: 72,
address: "London No. 1 Lake Park",
index:13,
children: [
{
key: 131,
name: "Jim Green",
age: 42,
address: "London No. 2 Lake Park",
index:131,
children: [
{
key: 1311,
name: "Jim Green jr.",
age: 25,
address: "London No. 3 Lake Park",
index:1311,
},
{
key: 1312,
name: "Jimmy Green sr.",
age: 18,
address: "London No. 4 Lake Park",
index:1312,
},
],
},
],
},
],
},
{
key: 2,
name: "Joe Black",
age: 32,
address: "Sidney No. 1 Lake Park",
index:2,
},
]);
//使用递归方法 //I 相当于数据
const onDelete= (l,key)=> {
const index = l.findIndex(child => child.key === key);
if (index !== -1) {
l.splice(index, 1);
} else {
l.forEach(child => {
onDelete(child.children, key);
});
}
}
使用递归遍历实现删除数据
最新推荐文章于 2024-05-31 10:46:10 发布