remove element from middle of array javascript code example

Example 1: remove array elements javascript

let forDeletion = [2, 3, 5]

let arr = [1, 2, 3, 4, 5, 3]

arr = arr.filter(item => !forDeletion.includes(item))
// !!! Read below about array.includes(...) support !!!

console.log(arr)
// [ 1, 4 ]

Example 2: javascript array remove middle

// example (remove middle element(s) in the array)
let yourArray = ["aaa", "bbb", "ccc", "ddd"];
yourArray.splice(2,1); // yourArray = ["aaa", "bbb", "ddd"]

// syntax:
// <array-name>.splice(<start-index>,<number-of-elements-to-remove>);

Example 3: 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;
}

Tags:

C Example