remove js array element code example

Example 1: remove an element from array

var colors = ["red", "blue", "car","green"];

// op1: with direct arrow function
colors = colors.filter(data => data != "car");

// op2: with function return value
colors = colors.filter(function(data) { return data != "car"});

Example 2: remove elemtns from an array with splice

var fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
document.getElementById("demo").innerHTML = fruits;

function myFunction() {
  fruits.splice(2, 2);
  document.getElementById("demo").innerHTML = fruits;
}