Two ways to remove an item in a specific in an array | Javascript

 //Mutating way
const muatatedArray = ['a','b','c','d','e'];
muatatedArray.splice(2,1)
console.log(muatatedArray) //['a','b','d','e']

//Non-mutating way
const nonMuatatedArray = ['a','b','c','d','e'];
const newArray = nonMuatatedArray.filter((item, index) => !( index === 2 ));
console.log(newArray) //['a','b','d','e'] 

AI Explanation

This code snippet demonstrates two ways of removing an element from an array at a specific index: mutating and non-mutating way. 1. Mutating way: - The `splice` method is used to remove an element from the `muatatedArray` at index 2 with a removal count of 1. - The `splice` method modifies the original array by removing the element at the specified index. - After removal, the mutated array is ['a', 'b', 'd', 'e']. 2. Non-mutating way: - The `filter` method is used on the `nonMuatatedArray` to create a new array `newArray`. - The `filter` method iterates over each element of the array and returns a new array with elements that pass the condition specified in the callback function. - In this case, the condition checks if the index is not equal to 2, and if true, the element at that index is kept in the new array. - After filtering, the new array is ['a', 'b', 'd', 'e']. - This method does not modify the original `nonMuatatedArray` and returns a new array with the desired elements.