Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

fruits = ["mango","apple","pine","berry"];

If I want to remove an element index wise, How to do that using JavaScript. Also you can use library as required.

like image 919
Hasanuzzaman Avatar asked Sep 15 '18 19:09

Hasanuzzaman


1 Answers

You can use splice to do the same. Syntax = array.splice(start_index, no_of_elements) Following is the command:

const fruits = ["mango","apple","pine","berry"]; // returns mutated array
const removed = fruits.splice(2, 1); // returns array of removed items
console.log('fruits', fruits);
console.log('removed', removed);

This will remove one element from index 2, i.e. after the operation fruits=["mango","apple","berry"];

like image 84
Jahnavi Paliwal Avatar answered Oct 30 '22 08:10

Jahnavi Paliwal