How to use array.filter() Method In Javascript
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.
// 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 ]
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: