remove items from an index position

Solutions on MaxInterview for remove items from an index position by the best coders in the world

showing results for - "remove items from an index position"
Regina
18 Jan 2021
1let vegetables = ['Cabbage', 'Turnip', 'Radish', 'Carrot']
2console.log(vegetables)
3// ["Cabbage", "Turnip", "Radish", "Carrot"]
4
5let pos = 1
6let n = 2
7
8let removedItems = vegetables.splice(pos, n)
9// this is how to remove items, n defines the number of items to be removed,
10// starting at the index position specified by pos and progressing toward the end of array.
11
12console.log(vegetables)
13// ["Cabbage", "Carrot"] (the original array is changed)
14
15console.log(removedItems)
16// ["Turnip", "Radish"]
17
Mathilda
27 Mar 2016
1let removedItem = fruits.splice(pos, 1) // this is how to remove an item
2
3// ["Strawberry", "Mango"]