remove array element javascript by id code example

Example 1: remove item at index in array javascript

// remove element at certain index without changing original
let arr = [0,1,2,3,4,5]
let newArr = [...arr]
newArr.splice(1,1)//remove 1 element from index 1
console.log(arr) // [0,1,2,3,4,5]
console.log(newArr)// [0,2,3,4,5]

Example 2: how to remove an element from an array javascript

let myArray = [3, 1, 4, 1, 5, 2];
let unwantedElementIndex = 3;

// This line removes 1 element from the array, starting at the unwantedElementIndex
myArray.splice(unwantedElementIndex, 1);

// If you have your custom test to remove an element just go for 
// Higher-Order functions
let myArray = ["[email protected]", "wrong@[email protected]", "..."];
const emailRegexp = ____;
let allCorrectEmails = myArray.filter(element => emailRegexp.test(element));

Tags:

Cpp Example