Delete object in array by parent id recursively

问题: 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 de...

问题:

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}
]

回答1:

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


回答2:

So this is my understanding about the problem:

  • Remove all the items in the given array based on the parent property value.
  • While removing each item make sure to remove the items having same parent property value of the removed item's id.

var savedArray = [
	{
		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
	}
];

function removeByIds(arr, parentIdsToBeRemoved) {
	// if there is no parentIdsToBeRemoved return whole array
	if (!parentIdsToBeRemoved || parentIdsToBeRemoved.length == 0) {
		return arr;
	}
	var tempIdsToBeRemoved = [];
	var newArr = arr.filter((item, index) => {
		if (parentIdsToBeRemoved.indexOf(item.parent) > -1) {
			tempIdsToBeRemoved.push(item.id);
		} else {
			return item;
		}
	});
	return removeByIds(newArr, tempIdsToBeRemoved);
}


function removeById(arr, id) {
  return arr.filter(item => {
    return item.id != id;
  })
}

var output = removeByIds(savedArray, ["12"]);
output = removeById(output, "12");
console.log(output);

  • 发表于 2019-03-13 05:02
  • 阅读 ( 238 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除