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

Jaydeep
  • 1,628
  • 1
  • 16
  • 28
Hasanuzzaman
  • 343
  • 1
  • 2
  • 11
  • 2
    Was about to downvote that question but it's actually not trivial to find the answer on SO itself. This website's search engine is definitely subpart, compared to google who was able to find the right answer page 1... on SO domain itself... – mpm Sep 15 '18 at 19:38
  • why vue in tags? – marzelin Sep 15 '18 at 19:39

1 Answers1

33

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

danday74
  • 45,909
  • 39
  • 198
  • 245
Jahnavi Paliwal
  • 1,373
  • 1
  • 10
  • 20