I want to remove an element from array, but splice method remove all elemts from star index till the end of array.
I have this code:
function basketItemRemove(obj, id) {
...
if (id == 0)
tempBasketList.splice(id, id + 1);
else
tempBasketList.splice(id, id);
...
}
I check id 0, because splice don’t remove with index 0. Then splice(id, id); properly work only with second element of array, but if element is third or higher then it delete all till the end of array.
The second argument to
spliceis how many elements to remove, not the point at which to stop removing them. If you want to remove one element, use1:It does, but not if that’s what you give for the second argument, because it means to remove zero items.
The signature of
splice(from the link above) explained:start: Where to start doing things (the index)deleteCount: How many to delete at that locationitem1,item2,item3: Optional items to add at that location (after having deleted, ifdeleteCount > 0)