Why does the splice method return undefined and doesn't remove the element at position 4 in the following code:
var excludedDepartmentsList = [1, 2, 3, 4, 5, 6];
var currentDepartmentId = 5;
var position = $.inArray(currentDepartmentId, excludedDepartmentsList);
if (position > -1) {
var q = excludedDepartmentsList.splice[position, 1];
return;
}
I have made a test here: http://jsfiddle.net/PnVEb/
.splice is a function and it should be called like excludedDepartmentsList.splice(position, 1) and not excludedDepartmentsList.splice[position, 1]. Note the brackets changed from [] to ().
Use () like below and it should return 5
excludedDepartmentsList.splice(position, 1)
fixed fiddle: http://jsfiddle.net/PnVEb/1/
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