

Unlike splice which mutates the data, filter creates a new array with elements that fit the condition. So if you need to handle duplicates, there's a more straightforward way we'll show you in the next method. You might've noticed there was more than one unicorn in the above code.īecause findIndex will only return the first element that satisfies the condition, we are still left with another unicorn. So here we see that the output for the removeItem function is an array of just fruits, and the original fruits array remains unchanged. Finally, We will need to return the variable newArray to be able to get the output. Recall how the splice method's first parameter is the index you want to change, and the second parameter is how many elements you want to remove in the array. To use the findIndex method, we will type in the condition we want it to satisfy.įinally, to make sure we found the unicorn, we will check that the variable index does not equal to -1, before splicing it to remove the item.

But if it returns -1, then it means there wasn't the element that fit the condition. This will find the first element that satisfies the condition function. We will create a function that takes an array and item as parameters.įirst, to copy the array, we will use the spread operator. We're going to copy the array, find the index, and splice it! const fruits = Ĭonst index = newArray.findIndex((item) => item = "🦄") Ĭonsole.log(removeItem(fruits, "🦄")) // output: Here, we will be using the arrow function syntax in ES6. Let's look back at the fruits array again. But what if we don't want to mutate the original array? Something to be extra careful about is when using splice, the original array is mutated, meaning you are changing the original array. Notice how the output of the splice function returns the unicorn in an array, whereas fruits array has changed to a unicorn-free fruit salad. const fruits = Ĭonsole.log(fruits.splice(2, 1)) // outputs: Ĭonsole.log(fruits) // output: The second parameter determines how many items you want to delete, which will be 1. To remove an item using splice, the first parameter is the index of the item we want to remove. Splice mutates the original codeīelow is our array filled with fruits and a unicorn. The return value of the splice method is a new array containing the deleted elements. Splice changes the contents of the array by removing, replacing, or adding new elements. Watch the tutorial here or click the video below to watch it! Method 1: Splice Methodįirst off, what does the splice method do?
DISCORD JS SPLICE ARGUMENTS HOW TO
In this tutorial, we are going to look at how to remove a specific item from an array using JavaScript's native array methods: splice and filter. How would you go about using JavaScript's native array methods to remove that product ID from your shopping cart array? Have you ever been stuck trying to remove a specific item from an array? Imagine this: you are working on a shopping cart delete feature where you need to delete an item that the user doesn't want anymore.
