2

I have an object like:

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

I want to delete the object with id of 2345666078, I am using this function but it's not working:

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

what's wrong with my code? also, how can I get/update the values for an object? for example how I am supposed to the title of 7697347548? or how I can update name of 234567869?

I appreciate any kind of help.

3
  • Can you tell us what is not working , the delete statement or the matching of the id ? Commented Jan 24, 2018 at 10:09
  • @Devesh the delete statement. Commented Jan 24, 2018 at 10:23
  • Possible duplicate of How to access nested JSON data Commented Jan 24, 2018 at 10:57

2 Answers 2

1

You could use Map and Splice to delete a nested element like this:

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)

And for updating the name property inside another element (Nested) you can do:

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)

Sign up to request clarification or add additional context in comments.

7 Comments

your update function has an error. seems to be a syntax error. jsfiddle.net/8a1ujncm see the console: Uncaught ReferenceError: Invalid left-hand side in assignment
@behz4d check your arrow symbol. You did = > instead of =>
@behz4d check this now. Also, I've updated the update function in my answer for better readability.
It was a relatively quick fix. I've updated my answer.
@behz4d If the answer worked for you as demonstrated here please consider accepting/upvoting it. :)
|
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); } 

2 Comments

how about updating it?
which type of updating you want.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.