remove an element from an array javascript by value 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: js remove object from array by value

let originalArray = [
    {name: 'John', age: 23, color: 'red'}, 
    {name: 'Ann', age: 21, color: 'blue'}, 
    {name: 'Mike', age: 13, color: 'green'}
];

let filteredArray = originalArray.filter(value => value.age > 18);

Example 3: js remove from array by value

const index = array.indexOf(item);
if (index !== -1) array.splice(index, 1);

Example 4: delete from array based on value javascript

var index = array.indexOf(item);
if (index !== -1) array.splice(index, 1);