I may be wrong on what I think .splice() is meant to do, but I thought it removed one element of an array. All I want to do here is remove "pears", but it doesn't work:
var my_array = ["apples","pears","bananas","oranges"];
my_array.splice($.inArray("pears",my_array));
$.each(my_array, function(k,v) {
document.write(v+"<br>");
});
Also at http://jsfiddle.net/jdb1991/nV95v/
You're missing two arguments:
$.inArray wants the second argument to be the subject arraysplice accepts a second argument to specify the number of elements to be deletedThe code becomes:
var my_array = ["apples","pears","bananas","oranges"];
my_array.splice($.inArray("pears", my_array), 1);
$.each(my_array, function(k,v) {
document.write(v+"<br>");
});
Live example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With