[Vuejs]-Delete an item in an object array by id

0👍

this.cellsListsData = this.cellsListsData.filter(function(element) {
    return element.id !== id;
  });

or Just Get the index of it and then this.cellsListsData.splice(index, 1);

0👍

This would require traversing through the whole array and finding the index of object with the given id. Once you find the id, you can simply splice the array from that index with offset 1.
Below is a quick snippet for doing that.

var index_to_delete;
data.forEach(function(value, index) {
   if (value.id == id_of_clicked_element) {
     index_to_delete = index;
   };
});
data.splice(index_to_delete, 1);

Note: Using filter will create a new array. So, if you are referencing the data array in some other variables, they will not get the new array. So, I recommend using the filter solution with caution. Also, some answers say to delete element at that index. This would remove that index from array and your array will be rendered inconsistent.

Leave a comment