Reputation: 1900
I'm having trouble figuring how to delete an item from array where the items are indexed in a manner similar to the following:
arr[32] = 123
arr[39] = 456
arr[92] = 789
...
The two ways I have tried to delete single, specific items from said array have resulted in all items being removed.
Attempted Method #1:
arr.splice(39, 1);
Attempted Method #2:
arr.forEach(function(val, key) {
if (val == 456) {
arr.splice(key, 1);
}
}
Now obviously this isn't exactly what my code looks like, but it shows what I've tried well enough. If I am missing any important details, or you want me to pick the code from source to see if it is within the source instead of the methodology, please ask
Upvotes: 0
Views: 40
Reputation: 386680
While splicing changes the length of the array and the given array seems to rely on indices, you could just set the item to undefined
.
var arr = [];
arr[32] = 123;
arr[39] = 456;
arr[92] = 789;
arr[39] = undefined;
console.log(arr[92]);
Upvotes: 2