javascript remove object from array by value code example

Example 1: remove all from array that matches

var array = [1,2,'deleted',4,5,'deleted',6,7];
var newarr = array.filter(function(a){return a !== 'deleted'})

Example 2: 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 3: javascript remove from array by index

//Remove specific value by index
array.splice(index, 1);

Example 4: locate and delete an object in an array

// we have an array of objects, we want to remove one object using only the id property
var apps = [{id:34,name:'My App',another:'thing'},{id:37,name:'My New App',another:'things'}];
 
// get index of object with id:37
var removeIndex = apps.map(function(item) { return item.id; }).indexOf(37);
 
// remove object
apps.splice(removeIndex, 1);

Example 5: 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 6: js remove from array by value

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

Tags:

C Example