JavaScript - Remove element from array (unset array element)
1) Method 1: splice
array.splice(position, number_of_keys);
Example:
OBS: The function doesn't preserve key association.
2) Method 2:
To preserve key association, you must do something like this:
var my_array = [1, 4, 5, 6];
var remove_key = 1;
var array_new = [];
for(var i in my_array) {
if(i != remove_key) {
array_new[i] = my_array[i];
}
}
1) Method 1: splice
array.splice(position, number_of_keys);
Example:
array.splice(1, 1);
OBS: The function doesn't preserve key association.
2) Method 2:
To preserve key association, you must do something like this:
var my_array = [1, 4, 5, 6];
var remove_key = 1;
var array_new = [];
for(var i in my_array) {
if(i != remove_key) {
array_new[i] = my_array[i];
}
}
No comments:
Post a Comment