通过父ID递归删除数组中的对象

时间:2019-03-12 05:20:50

标签: javascript

目前,我正在研究递归方法。

“数组”具有“对象”,其自身具有parentId和Id。

我想做一个函数:当我选择一个对象时,想通过parentId删除每个子对象。同时应删除孩子的孩子。

现在,此代码可用于某些子对象,但不适用于所有子对象。而且我不知道为什么它不起作用。

您能帮我解决这个问题吗?

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

尝试了代码,但仍无法在父对象和子对象上工作。

这是删除ID为'12'的对象时代码的结果

因为“ 123”,“ 1234”的父ID为“ 12”,

“ 123456789 1234567890”,“ 12345、123456”应删除

因为它们是“ 123”和“ 1234”的子代

,也不会删除'12'。

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

2 个答案:

答案 0 :(得分:2)

这是我对问题的理解:

  • 根据父级属性值删除给定数组中的所有项目。
  • 在删除每个项目时,请确保删除与已删除项目的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);

答案 1 :(得分:0)

我认为这应该可以满足您的需求。如果您有循环依赖性,则可能会中断。它仍然使用递归,但是我将递归移到了filter函数中。

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);
    }
}

在您的示例中,您省略了也应删除的1234567123456781234567123456的子代,已被删除,123456781234567的子代