An item in an array remove | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

An item in an array remove

I'm making a filter for change an item undefined but why there is an item remove,too This is the code : https://code.sololearn.com/WyFMfV8mcRrk/?ref=app Explain to me why this is happened ? And thanks before

5th Feb 2020, 1:22 PM
Hafizd Jubaidir
Hafizd Jubaidir - avatar
3 Answers
+ 2
first argument is starting index. second argument is amount of item to be removed. now both index and amount is i at 2, so it removes two elements. as you want to replace undefined, you should use 1 instead of i as second argument. the amount can be 0, if zero element to remove, means insert before that index. see Mozilla doc example. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice when you want to understand a JavaScript function, always read Mozilla doc.
5th Feb 2020, 2:30 PM
Gordon
Gordon - avatar
+ 1
because the second argument of splice is how many element to take out. If you want to remove one only, .... splice(i, 1,...)
5th Feb 2020, 2:09 PM
Gordon
Gordon - avatar
0
Gordon Isn't the second set of splice an index to be erased? Or the amount of items that must be removed?
5th Feb 2020, 2:17 PM
Hafizd Jubaidir
Hafizd Jubaidir - avatar