2017-02-21 64 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);