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:

Methods to Delete Middle Element from an Array

Table of Content

  • Methods to Delete Middle Element from an Array
  • Using JavaScript Spread Operator
  • Using the JavaScript splice() method
  • Using the JavaScript delete operator
  • Using JavaScript array.filter() Method
  • Using Array.slice() method

Using JavaScript Spread Operator

The Spread operator allows an iterable to expand in places where 0+ arguments are expected. It is mostly used in the variable array where there is more than 1 value is expected. 

Syntax:

let variablename1 = [...value]; 

Example: In this example, we will use the JavaScript spread operator to create a new array without the middle element.

Javascript
// Input array
let Arr = [1, 2, 3, 4, 5, 6];

// Index of deleting element
let middleIndex = Math.floor(Arr.length / 2);
let newArray = 
    [...Arr.slice(0, middleIndex), ...Arr.slice(middleIndex + 1)]

console.log(newArray);

Output
[ 1, 2, 3, 5, 6 ]

Using the JavaScript splice() method

The JavaScript Array splice() Method is an inbuilt method in JavaScript that is used to modify the contents of an array by removing the existing elements and/or by adding new elements.

Syntax:

Array.splice( index, remove_count, item_list )

Example: In this example, we will use the JavaScript splice method to delete the middle element.

Javascript
// Input array
let Arr = [1, 2, 3, 4, 5, 6];

// Index of deleting element
let middleIndex = Math.floor(Arr.length / 2);
Arr.splice(middleIndex, 1);

console.log(Arr);

Output
[ 1, 2, 3, 5, 6 ]

Using the JavaScript delete operator

The delete operator is used to delete JavaScript objects and object properties.

Syntax:

delete object;
delete object[property];

Example: In this example, we will use the JavaScript delete operator to delete the middle element.

Javascript
// Input array
let Arr = [1, 2, 3, 4, 5, 6];

// Index of deleting element
let index = Math.floor(Arr.length / 2);
delete Arr[index];

console.log(Arr);

Output
[ 1, 2, 3, <1 empty item>, 5, 6 ]

Using JavaScript array.filter() Method

The JavaScript Array filter() Method is used to create a new array from a given array consisting of only those elements from the given array which satisfy a condition set by the argument method. 

Syntax: 

array.filter(callback(element, index, arr), thisValue)

Example: In this example, we will use the JavaScript array.filter() method to create a new array without the middle element.

Javascript
// Input array
let Arr = [1, 2, 3, 4, 5, 6];

// Index of deleting element
let middleIndex = Math.floor(Arr.length / 2);
let newArray = 
    Arr.filter((e, i) => i !== middleIndex)

console.log(newArray);

Output
[ 1, 2, 3, 5, 6 ]

Using Array.slice() method

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.

JavaScript
// 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 ]