我有一个数组存储在我的本地存储中。它是动态的。我将数据连同一个ID在每次输入后递增。用户可以选择删除,因此,我应该从数组中删除相应的元素。但是,我希望ID保持按升序排列。例如:在删除JSON数组后减少ID
var jsonObj = [{'id':'1','name':'Ray','email':'[email protected]'},
{'id':'2','name':'Steve','email':'[email protected]'},
{'id':'3','name':'Albert','email':'[email protected]'},
{'id':'4','name':'Christopher','email':'[email protected]'}]
我正在为上面的数组创建HTML div。如果Steve删除了他的详细信息,我希望该阵列如下所示:
var jsonObj = [{"id":1,"name":"Ray","email":"[email protected]"},
{"id":2,"name":"Albert","email":'[email protected]"},
{"id":3,"name":"Christopher","email":"[email protected]"}]
以下代码不能相应地工作。
for (var i=0; i<jsonObj.length; i++) {
//delete function is working fine.
jsonObj[i].id--;
break;
}
http://stackoverflow.com/questions/8310270/reindex-javascript-array-object-after-纯JS做删除键 – mplungjan