For "Arrays":
If you know the index:
array.splice(index, 1);
If you know the value:
function removeItem(array, value) {
var index = array.indexOf(value);
if (index > -1) {
array.splice(index, 1);
}
return array;
}
The most upvoted answer for delete
works well in case of objects but not for the real arrays. If I use delete
it removes elements from loops but keeps the element as empty
and length of array wont change. This may be a problem in some scenarios.
For example, if I do myArray.toString() on myArray after removal via delete
, it creates an empty entry, i.e. ,,
.