May 12, 2021
← Back to Blog  |  Home

Removing elements from arrays

⏰ 1 min read

let list = ["item_0", "item_1", "item_2", "item_3"]

In the first two methods, I will be removing the second element, which is "baz" from the array. The end result will be ["item_0", "item_2", "item_3"]

Removing a specific element by value
// 1st value passed: the start element
// 2nd value passed: how many elements to be removed.
var final = list.splice(1,1)
var final = list.splice(list.indexOf("item_1"),1)
Removing a specific element by index
delete list[1];
Removing the last element of the array
list.length = 3
list.pop()
Removing the first element of the array
list.shift()