javscript remove from arra code example

Example 1: remove a particular element from array

var colors = ["red","blue","car","green"];
var carIndex = colors.indexOf("car");//get  "car" index
//remove car from the colors array
colors.splice(carIndex, 1); // colors = ["red","blue","green"]

Example 2: 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 3: js remove element from array

let arrDeletedItems = array.splice(start[, deleteCount[, item1[, item2[, ...]]]])

Example 4: remove element from array javascript

let values = [1,2,3,4,5,7,8,9,10]

// only keep n values from an array
values.length = 5

Example 5: remove element from array js

var fruits = ["Banana", "Orange", "Apple", "Mango"];

fruits.pop();              
// Removes the last element ("Mango") from fruits

Example 6: remove element from array

array.remove(number);
array.remove(number);