使用它的ID删除/更新嵌套对象中的对象

时间:2018-01-24 10:07:59

标签: javascript

我有一个像这样的对象:

{
'id': '234567869',
'name': 'Lao Lao',
'title': 'general manager',
'children': [{
    'id': '467876756634',
    'name': 'Bo Miao',
    'title': 'department manager'
}, {
    'id': '2345666078',
    'name': 'Su Miao',
    'title': 'department manager',
    'children': [{
        'id': '898735342',
        'name': 'Tie Hua',
        'title': 'senior engineer'
    }, {
        'id': '7697347548',
        'name': 'Hei Hei',
        'title': 'senior engineer',
        'children': [{
            'id': '123415640',
            'name': 'Pang Pang',
            'title': 'engineer'
        }, {
            'id': '1237450976',
            'name': 'Xiang Xiang',
            'title': 'UE engineer'
        }]
    }]
}, {
    'id': '6968756535',
    'name': 'Yu Jie',
    'title': 'department manager'
}, {
    'id': '236448654',
    'name': 'Chun Miao',
    'title': 'department manager'
}, {
    'id': '356898765',
    'name': 'Yu Tie',
    'title': 'department manager'
}]}

我想要删除ID为2345666078的对象,我正在使用此功能,但它无效:

function deleteNode(idToFind, bigObjectToSearch) {
    var i, currentChild, result;
    if (idToFind == bigObjectToSearch.id) {
        delete bigObjectToSearch;
        return true;
    } else if (bigObjectToSearch.children) {
        for (i = 0; i < bigObjectToSearch.children.length; i += 1) {
            currentChild = bigObjectToSearch.children[i];
            // Search in the current child
            if (deleteNode(idToFind, currentChild)) {
                break;
            }
        }
        return false;
    }
    return false;
}

我的代码出了什么问题?另外,我如何获取/更新对象的值?例如我应该如何title 7697347548?或者我如何更新name的{​​{1}}?

我感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

var obj = {
  id: "234567869",
  name: "Lao Lao",
  title: "general manager",
  children: [
    {
      id: "467876756634",
      name: "Bo Miao",
      title: "department manager"
    },
    {
      id: "2345666078",
      name: "Su Miao",
      title: "department manager",
      children: [
        {
          id: "898735342",
          name: "Tie Hua",
          title: "senior engineer"
        },
        {
          id: "7697347548",
          name: "Hei Hei",
          title: "senior engineer",
          children: [
            {
              id: "123415640",
              name: "Pang Pang",
              title: "engineer"
            },
            {
              id: "1237450976",
              name: "Xiang Xiang",
              title: "UE engineer"
            }
          ]
        }
      ]
    },
    {
      id: "6968756535",
      name: "Yu Jie",
      title: "department manager"
    },
    {
      id: "236448654",
      name: "Chun Miao",
      title: "department manager"
    },
    {
      id: "356898765",
      name: "Yu Tie",
      title: "department manager"
    }
  ]
};

deleteNode("2345666078",obj);

function deleteNode(id,obj){
 obj.children.forEach((d,i)=>{if(d.id == "2345666078"){obj.children.splice(i,1)}});  console.log(obj);
}

答案 1 :(得分:1)

您可以使用MapSplice 删除 嵌套元素,如下所示:

&#13;
&#13;
obj = {
'id': '234567869',
'name': 'Lao Lao',
'title': 'general manager',
'children': [{
    'id': '467876756634',
    'name': 'Bo Miao',
    'title': 'department manager'
}, {
    'id': '2345666078',
    'name': 'Su Miao',
    'title': 'department manager',
    'children': [{
        'id': '898735342',
        'name': 'Tie Hua',
        'title': 'senior engineer'
    }, {
        'id': '7697347548',
        'name': 'Hei Hei',
        'title': 'senior engineer',
        'children': [{
            'id': '123415640',
            'name': 'Pang Pang',
            'title': 'engineer'
        }, {
            'id': '1237450976',
            'name': 'Xiang Xiang',
            'title': 'UE engineer'
        }]
    }]
}, {
    'id': '6968756535',
    'name': 'Yu Jie',
    'title': 'department manager'
}, {
    'id': '236448654',
    'name': 'Chun Miao',
    'title': 'department manager'
}, {
    'id': '356898765',
    'name': 'Yu Tie',
    'title': 'department manager'
}]}

let newObject = {};

function deleteElement(object,passedId) {

object.children.map((element, index) => {
	if (element.id === passedId){     
  	console.log(index)
    object.children.splice(index, 1)
  }
  if (element.children != null){      // condition for checking Nesting
  	deleteElement(element, passedId)
  }
})
	//console.log(object)
  newObject = object
}

deleteElement(obj,"7697347548");

console.log(newObject)
&#13;
&#13;
&#13;

对于更新 另一个元素(嵌套)中的name属性,您可以执行以下操作:

&#13;
&#13;
obj = {
'id': '234567869',
'name': 'Lao Lao',
'title': 'general manager',
'children': [{
    'id': '467876756634',
    'name': 'Bo Miao',
    'title': 'department manager'
}, {
    'id': '2345666078',
    'name': 'Su Miao',
    'title': 'department manager',
    'children': [{
        'id': '898735342',
        'name': 'Tie Hua',
        'title': 'senior engineer'
    }, {
        'id': '7697347548',
        'name': 'Hei Hei',
        'title': 'senior engineer',
        'children': [{
            'id': '123415640',
            'name': 'Pang Pang',
            'title': 'engineer'
        }, {
            'id': '1237450976',
            'name': 'Xiang Xiang',
            'title': 'UE engineer'
        }]
    }]
}, {
    'id': '6968756535',
    'name': 'Yu Jie',
    'title': 'department manager'
}, {
    'id': '236448654',
    'name': 'Chun Miao',
    'title': 'department manager'
}, {
    'id': '356898765',
    'name': 'Yu Tie',
    'title': 'department manager'
}]}

let updatedObject = {};

function update(object,passedId) {

  object.children.map((element, index) => {
    if (element.id === passedId){
      console.log(index)
      object.children[index].name = "New Name";
      return;
    }
    if (element.children != null){      // condition for checking Nesting
      update(element, passedId)
      updatedObject = object;
    }
  })
  
  if (object.id == passedId){
    object.name = "New Name";
    return;
  }
  
	updatedObject = object
}

update(obj,"234567869");
console.log(updatedObject)
&#13;
&#13;
&#13;