Currently I'm working on recursive methods.
'The array' has 'objects' which have parentId and Id for itself.
I wanna make a function: when I choose one object, wanna delete every child object by parentId. also at the same time child's child should be deleted.
Now this code works on some child objects, but not for all. and I don't know why does it not working.
Could you please help me to figure out this problem?
function deleteMindMap(obj) {
alert('Before Delete : ' + JSON.stringify(savedArray));
savedArray = deleteUsingParentId(savedArray, obj.id);
alert('After Delete : ' + JSON.stringify(savedArray));
//Rewriting to firebase
mindRef.remove();
writeMindMap(savedArray);
}
function deleteUsingParentId(data, parentId) {
var updatedArray = savedArray.filter((item) => {
return item.parent != parentId;
})
return updatedArray;
}
tried the code but still not working on parent object and child's child object.
this is the result of the code when I deleted the object of id '12'
cause '123', '1234' have parent id '12',
'123456789 1234567890' , '12345, 123456' should be deleted
cause they are child of '123' and '1234'
and also '12' is not deleted.
Before Delete : [
{"afterX":485,"afterY":271,"id":"1","kind":"line","parent":"1","x":448,"y":220},
{"afterX":643,"afterY":276,"id":"12","kind":"line","parent":"1","x":490,"y":278},
{"afterX":732,"afterY":238,"id":"123","kind":"line","parent":"12","x":659,"y":283},{"afterX":708,"afterY":413,"id":"1234","kind":"line","parent":"12","x":668,"y":291},
{"afterX":847,"afterY":390,"id":"12345","kind":"line","parent":"1234","x":721,"y":418},
{"afterX":791,"afterY":494,"id":"123456","kind":"line","parent":"1234","x":715,"y":427},
{"afterX":904,"afterY":520,"id":"1234567","kind":"line","parent":"123456","x":810,"y":503},
{"afterX":944,"afterY":301,"id":"12345678","kind":"line","parent":"1234567","x":913,"y":521},
{"afterX":796,"afterY":136,"id":"123456789","kind":"line","parent":"123","x":736,"y":230},
{"afterX":869,"afterY":227,"id":"1234567890","kind":"line","parent":"123","x":752,"y":245}]
After Delete : [{"afterX":485,"afterY":271,"id":"1","kind":"line","parent":"1","x":448,"y":220},
{"afterX":643,"afterY":276,"id":"12","kind":"line","parent":"1","x":490,"y":278},
{"afterX":847,"afterY":390,"id":"12345","kind":"line","parent":"1234","x":721,"y":418},
{"afterX":791,"afterY":494,"id":"123456","kind":"line","parent":"1234","x":715,"y":427},
{"afterX":904,"afterY":520,"id":"1234567","kind":"line","parent":"123456","x":810,"y":503},
{"afterX":944,"afterY":301,"id":"12345678","kind":"line","parent":"1234567","x":913,"y":521},
{"afterX":796,"afterY":136,"id":"123456789","kind":"line","parent":"123","x":736,"y":230},
{"afterX":869,"afterY":227,"id":"1234567890","kind":"line","parent":"123","x":752,"y":245}
]
I think this should do what you're looking for. It may break if you have circular dependencies. It still uses recursion but I moved the recursion inside the filter
function.
function deleteChild(id){
function filter(_target){
let toDelete = [];
for(let i = 0; i < savedArray.length; i++){
if(savedArray[i].id == _target || savedArray[i].parent == _target){
toDelete.push(i);
if(savedArray[i].id != _target){
toDelete = toDelete.concat(filter(savedArray[i].id).slice(1));
}
}
}
return toDelete;
}
const targets = filter(id).sort();
for(let i = targets.length - 1; i >= 0; i--){
savedArray.splice(targets[i],1);
}
}
In your example you omitted 1234567
and 12345678
who should also get deleted. 1234567
is a child of 123456
, who got deleted and 12345678
is a child of 1234567
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments