How do you remove a specific element from an array?

Hi, I am doing a project for school involving arrays. And i am wondering if there’s a way to remove a specific element from an array. Like say I have an array with 5 elements. Is there a way to remove the third elment and have the array now have the former fourth element as the third elment and so on. Thanks for taking the time to read.

@selmatheseal ,

There are quite a number of ways to aid your problem. Depends on your future problem cases.
But for the time being, I’d recommend using .splice , as they rather useful for array problems.

// Example function removeElement(arr, position) { return arr.splice(position); } let arrList = ['a','b','c','d','e']; arrList = removeElement(arrList, 1); console.log(arrList); arrList = removeElement(arrList, 1); console.log(arrList);
1 Like


I’d argue that this is a bad practice because you’re suggesting to use splice() which mutates original array.

const listOfThings = [1, 'a', 3, 4, 'c', 'j']; function isString(item) { return typeof item === "string"; } function isNumber(item) { return typeof item === "number"; } function thirdRemoved(_, index) { return index !== 2; } const listOfStrings = listOfThings.filter(isString); const listOfNumbers = listOfThings.filter(isNumber); const listWithout3rd = listOfThings.filter(thirdRemoved)
1 Like

Thanks for the additional inputs. I believe is a great help to @selmatheseal and certainly, no doubt, I have homework to do as well.

Nonetheless, thank you and keep the suggestions coming for one another’s improvements =)