I have created an empty json object having an array itemlist(which further contains itemid and title of an item) by this:
var jsonObj = {};
jsonObj.itemlist=[];
jsonObj.itemlist.push({});
Firstly, have i done the declaration correctly?
Secondly, the title and itemid are generated dynamically so i need to add them to the itemlist array. I tried this but it keeps only one array element:
jsonObj.itemlist['title']=gentitle;
jsonObj.itemlist['itemid']=genitemid;
How can i add multiple elements (not all at once) if i have an empty array of itemlists?
Also, i also need to remove a particular array element based on the title of the element. How can that be done? I think the splice and delete function can be used for this, but how can i find the index of that element?
since you already pushed a empty object into the array, you need to modify that object:
To add more objects, you can do the same thing: push in an empty object, then modify that object. Or, you can create an object, modify it, then push it into the list.
To delete objects with certain attribute value:
Note that you need to go backward, otherwise the
splicewill mess up the array indexes