+ 1
Remove an element from an array?
Certain element, of course.
2 Antworten
+ 3
By using splice().
Before that you need to find the index of your element.
splice is for editing arrays not just for deleting.
First argument is the index.
Second one is how many of them you want to delete. For example if you want to replace it with something else your delete count would be zero and the new value would be the third argument.
array.splice(array.indexOf(element), 1);
+ 3
Toni: Nice explanation 👍
Just one thing, maybe you want to test the script in code playground a little bit? You are very close actually.
Just in case you can't figure it out, here is a demo for you:
https://code.sololearn.com/WLoN67Z1IWwI/?ref=app
Ric: For your enrichment, apart from splice(), there are another four commonly used array methods:
https://code.sololearn.com/W3cNMx37R657/?ref=app
Depending what problem you are going to solve, you use different methods.
For example, for queue (first in first out), you use shift() push() pair
For another example, for stack (last in first out), you use push() pop() pair