How to use Array.slice() method In Javascript
The JavaScript Array.slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included). We can utilize this method to exclude the middle element from the array.
// Input array
let arr = [1, 2, 3, 4, 5, 6];
// Index of the middle element
let middleIndex = Math.floor(arr.length / 2);
// Delete the middle element using Array.slice() method
let newArray = [...arr.slice(0, middleIndex), ...arr.slice(middleIndex + 1)];
console.log(newArray);
Output
[ 1, 2, 3, 5, 6 ]
JavaScript Program to Delete Middle Element from an Array
In this article, we will learn different approaches to how we can delete the middle element from a given array. We will use these JavaScript methods to achieve this: