Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

javascript how to remove an object from nested json array

Former Member
0 Likes
4,237

Hello,

Im facing a small issue to remove an object from nested json array which will get generated dynamically. Below is the json..

Actually my requirement is like, I have a JSON Tree, and user can add or delete multiple nodes..

[{
	"mainTxt": "test",
	"subTxt": "SubTxt1",
	"nodes": [{
		"mainTxt": "test",
		"subTxt": "SubTxt2",
		"nodes": [{
			"mainTxt": "test",
			"subTxt": "test Operations",
			"nodes": [{
				"mainTxt": "test",
				"subTxt": "test Operations",
				"nodes": [{
					"mainTxt": "test",
					"subTxt": "test Operations"
				}]
			}]
		}]
	}, {
		"mainTxt": "test",
		"subTxt": "SubTxt3",
		"nodes": [{
			"mainTxt": "test",
			"subTxt": "test Operations",
			"nodes": [{
				"mainTxt": "test",
				"subTxt": "test Operations"
			}]
		}]
	}, {
		"mainTxt": "test",
		"subTxt": "india"
	}, {
		"mainTxt": "test",
		"subTxt": "SubTxt4"
	}, {
		"mainTxt": "test",
		"subTxt": "SubTxt5"
	}]
}]

Here the below code blocks may get added or removed dynamically

"nodes": [{
				"mainTxt": "test",
				"subTxt": "test Operations"
			}]
and
{
		"mainTxt": "test",
		"subTxt": "SubTxt5"
	}

How can i remove above blocks which got added dynamically and after removing again i need to form the same JSON array.

I tried multiple ways in writing a loop by finding index of that particular object in array and tried to remove using splice() and delete() method. But it didn't worked properly.

Can someone please help me if there is any simple fix?

Thank you in advance..

1 REPLY 1
Read only

0 Likes
839

Fixed the issue with below Code..

removeTile:function(oEvt){
        this._deleteRecord(globalArray, selObject);
        _oModel.setData(oModelData);},
    _deleteRecord:function(items, record){if(items !== undefined){for(var i=0;i< items.length;i++){if(items[i]=== record){
                    items.splice(i,1);break;}else{
                    this._deleteRecord(items[i].nodes, record);}}}}