I have an array stored in my local storage. It is dynamic. I'm storing the data along with an ID which gets incremented after every entry. User has an option of deleting, hence, I'm supposed to remove the corresponding element from the array. But, I want the ID to remain in ascending order. Ex:
var jsonObj = [{'id':'1','name':'Ray','email':'ray@gmail.com'},
{'id':'2','name':'Steve','email':'steve@gmail.com'},
{'id':'3','name':'Albert','email':'albert@gmail.com'},
{'id':'4','name':'Christopher','email':'chris@gmail.com'}]
I'm creating HTML divs for the above array. In case, Steve deletes his details, I want the array to be like:
var jsonObj = [{"id":1,"name":"Ray","email":"ray@gmail.com"},
{"id":2,"name":"Albert","email":'albert@gmail.com"},
{"id":3,"name":"Christopher","email":"chris@gmail.com"}]
The following code doesn't work accordingly.
for (var i=0; i<jsonObj.length; i++) {
//delete function is working fine.
jsonObj[i].id--;
break;
}