2017-02-21 62 views
2

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

http://stackoverflow.com/questions/8310270/reindex-javascript-array-object-after-純JS做刪除鍵 – mplungjan

回答

1

你只需要聲明一個新的變量在for循環,你將遞增,並分配這個值作爲其ID

for (var i=0, id=1; i<jsonObj.length; i++, 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]'}]; 
 

 

 
console.log(jsonObj); 
 

 
jsonObj.splice(1, 1); 
 

 

 
for (var i=0, id=1; i<jsonObj.length; i++, id++) { 
 
\t jsonObj[i].id = id; 
 
} 
 

 
console.log(jsonObj);

1

一個非常簡單的方法可以是:

// the index of the deleted element 
const delIndex = 2; 

// reindex all entries starting from deleted one 
for (var i=delIndex+1; i<jsonObj.length; i++) { 
    jsonObj[i].id = i + 1; 
} 

的ID基本上與陣列索引對應反正。因此,我們可以用相應的索引(+1開始,而不是像數組索引)覆蓋它,而不是嘗試重新計算id。

0

獲取刪除的索引,並將其分配到的i值環

for (var i=deletedIndex; i<jsonObj.length; i++) { 
     jsonObj[i].id--; 
    } 
3

你可以只從給定的指標迭代和遞減id財產。

function deleteItem(i) { 
 
    array.splice(i, 1); 
 
    while (i < array.length) { 
 
     array[i].id--; 
 
     i++; 
 
    } 
 
} 
 

 
var array = [{ 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]' }]; 
 

 
deleteItem(1); 
 
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

2

如果從0開始,那麼你甚至不需要的ID

雷是第0個元素,克里斯托弗是第三

刪除阿爾伯特和克里斯托弗2日

var jsObj = [{'name':'Ray','email':'[email protected]'}, 
 
      {'name':'Steve','email':'[email protected]'}, 
 
      {'name':'Albert','email':'[email protected]'}, 
 
      {'name':'Christopher','email':'[email protected]'}] 
 
for (var i=0;i<jsObj.length;i++) { 
 
    document.write("<br>ID"+(i+1)+": "+jsObj[i].name) 
 
} 
 
document.write("<hr/>"); 
 
jsObj.splice(2, 1); // Bye bye Albert 
 
for (var i=0;i<jsObj.length;i++) { 
 
    document.write("<br>ID"+(i+1)+": "+jsObj[i].name) 
 
}

更多信息

Reindex javascript array/object after removing a key

+0

我做了完全相同的事情,但我有一個用戶編輯選項,用戶可以編輯任何細節。元素之間的唯一性丟失了,我被命令使用ID。謝謝你的迴應先生。 – Venky

+0

你可以顯示id作爲(i + 1)在一個循環中 – mplungjan

+0

我會按照那個 – Venky

1

您的要求必須使用接頭方法中的JavaScript

array.splice(index_you_wantto_delete,count) 

ex:-jsonObj.splice(1,1); 

拼接()方法增加了從陣列/刪除項/,

+0

問題是不要刪除。這是別的。無論如何感謝您的幫助:) – Venky

1

這裏是一個冗長的溶液解釋過程一步一步

1-刪除元件

2 - 更新索引,如果元件被發現和刪除

/** 
* 
* @param array list Your list 
* @param int elementID The id of the element you want to remove 
* @returns list The list with the element removed and the indexes rearanged 
*/ 
var deleteElement = function (list, elementID) { 
    var removedIndex = false; 
    for (var index = 0; index < list.length; index++) { 
     if (list[index]['id'] === elementID) { 
      list.slice(index, 1); 
      removedIndex = index; 
      break; 
     } 
    } 
    if (removedIndex !== false) { 
     //Get last id 
     var lastElement = (removedIndex === 0) ? null : list[removedIndex - 1]; 
     // Set to 0 if the first element was removed 
     var lastID = (lastElement === null) ? 0 : lastElement.id; 
     for (var i = removedIndex; i < list.length; i++) { 
      list[i].id = ++lastID; 
     } 
    } 
    return list; 
}; 
0

請嘗試以下方法。希望它的作品!

// index of the deleted item 
var itemDeleted = 2; 

// create a for loop from deleted index till last 
for (var i = itemDeleted-1; i < jsonObj.length; i++) { 
    jsonObj[i].id = i+1; 
} 
1

您可以通過使用地圖

const jsonObj = [{ 
 
    'name': 'Ray', 
 
    'email': '[email protected]' 
 
    }, 
 
    { 
 
    'name': 'Steve', 
 
    'email': '[email protected]' 
 
    }, 
 
    { 
 
    'name': 'Albert', 
 
    'email': '[email protected]' 
 
    }, 
 
    { 
 
    'name': 'Christopher', 
 
    'email': '[email protected]' 
 
    } 
 
]; 
 

 
jsonObj.splice(1, 1); 
 

 
const newObj = jsonObj.map((c, i) => ({ 
 
    name: c.name, 
 
    email: c.email, 
 
    id: i + 1 
 
})); 
 

 
console.log(newObj);