Removing item from an array? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Removing item from an array?

Hi fellas. I have this situation, on my form when the user checks first label (checkbox) I put it in an array "answer" each next one that the user selects is placed in an array and so on. Now I would like that when the user wants to uncheck some label, that same one should be removed from an array. In this moment when I unchecked some answer it is added to an array again. My boss said me to use this documentation, to try with .filter and give me this https://stackoverflow.com/questions/5767325/how-can-i-remove-a-specific-item-from-an-array. Also he left comment in onClick on my checkbox.js. I send code. https://pastebin.com/gSyC9zMf Tnx in advance!

21st Dec 2021, 10:30 PM
Filip
Filip - avatar
1 Answer
+ 1
There are two approach :- 1. You can use .filter method as array.filter((item)=> {conditions}); 2. Or can use .indexof(element) and then remove it from the array given using .splice(indexOfEl, 1) NOTE: both methods will give unexpected results if array has identical elements.
23rd Dec 2021, 4:59 AM
Anshuman Singh
Anshuman Singh - avatar