Remove an element from an array? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 1

Remove an element from an array?

Certain element, of course.

10th Jan 2019, 9:04 PM
Ric2908
Ric2908 - avatar
2 ответов
+ 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);
10th Jan 2019, 10:24 PM
Toni Isotalo
Toni Isotalo - avatar
+ 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
11th Jan 2019, 1:06 AM
Gordon
Gordon - avatar