How can I remove an array element by index,using javaScript? [duplicate]

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"];

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)