DEV Community

Discussion on: Basic Javascript: Removing Elements from an Array

Collapse
 
miku86 profile image
miku86

I like to do it the functional way with filter().

const fruits = ["apple", "banana", "cherry"];

// remove by value (could be multiple occurrences)
const removedBananaByValue = fruits.filter((fruit) => fruit !== "banana");

// remove by index (always at max. one occurrence)
const removedBananaByIndex = fruits.filter((fruit, index) => index !== 1);