I hope that I understand your question correctly: do you want to delete one element of the array
var index = $.inArray(value, array);
if (index >= 0) {
array.splice(index, 1);
}
otherwise
$.each(arrayWithValuesToRemove, function (index, element) {
var index = $.inArray(element, array);
if (index < 0) {
return;
}
array.splice(index, 1);
});
if you just want random deletion
var desiredIndex = Math.floor(Math.random() * array.length);
array.splice(desiredIndex, 1);
if you want to keep the index, but not its value (in combination with any of the above examples):
delete array[index];
if you want to write your own comparison method (for example, an object as a value):
var compareFunction = function (element) {
};
var getIndexFunction = function (array) {
for (var i = 0; i < array.length; i++) {
var element = array[i];
if (compareMethod(element)) {
return i;
}
}
return -1;
};
var index = getIndexFunction(myArray);
if (index >= 0) {
myArray.splice(index, 1);
}
source
share