我有一個數組存儲在我的本地存儲中。它是動態的。我將數據連同一個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