Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the splice method return undefined when removing at a valid index position?

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/

like image 932
Kenci Avatar asked Nov 20 '25 18:11

Kenci


1 Answers

.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/

like image 191
Selvakumar Arumugam Avatar answered Nov 22 '25 06:11

Selvakumar Arumugam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!