how to remove an object from array in javascript code example

Example 1: javascript remove from array by index

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

Example 2: remove item from array javascript

const array = [2, 5, 9];

console.log(array);

const index = array.indexOf(5);
if (index > -1) {
  array.splice(index, 1);
}

// array = [2, 9]
console.log(array);

Example 3: javascript delete object from array

someArray.splice(x, 1);

Example 4: remove object from array javascript

someArray.splice(x, 1);

Example 5: how to delete object in array

let array = [1,2,3];
let item = array.indexOf(2)
let deleteCount = 1;
array.splice(item, deleteCount)

Example 6: javascript remove object from array

var array = ['Object1', 'Object2'];

// SIMPLE
	array.pop(object); // REMOVES OBJECT FROM ARRAY (AT THE END)
	// or
	array.shift(object); // REMOVES OBJECT FROM ARRAY (AT THE START)

// ADVANCED
	array.splice(position, 1);
	// REMOVES OBJECT FROM THE ARRAY (AT POSITION)

		// Position values: 0=1st, 1=2nd, etc.
		// The 1 says: "remove 1 object at position"

Tags:

Cpp Example