How to remove an array's element by its index?
For example
fruits = ["mango","apple","pine","berry"];
Remove element fruits[2] to get
fruits = ["mango","apple","berry"];
You can use splice
as: array.splice(start_index, no_of_elements_to_remove)
. Here's the solution to your example:
const fruits = ["mango","apple","pine","berry"]; const removed = fruits.splice(2, 1); // Mutates fruits and returns array of removed items console.log('fruits', fruits); // ["mango","apple","berry"] console.log('removed', removed); // ["pine"]
This will remove one element from index 2, i.e. after the operation fruits=["mango","apple","berry"];