Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why there is no a straighfoward "remove" method in javascript array?

Tags:

javascript

I'm not very experienced in javascript. I always have a question which is: why javascript array doesn't have a remove() method?

Isn't that good to have APIs as:

remove(index) : delete an item by its index and return deleted element

remove(func): delete items who match the criteria specified by the func and return a array of deleted items, for example:

let deleteStudents = studentArray.remove(s => s.age < 18);

so why there are no such useful methods in javascript and we have to use non-intuitive splice method?


1 Answers

There is. But the function is called splice.

To remove item at index n:

myArray.splice( n, 1 );
like image 163
Dai Avatar answered Mar 12 '26 03:03

Dai