Thank you for that! I was also stuck on this problem…
Can you please explain why the deleteCount parameter is logged as ‘1’ in you splice function? I thought I understood that the first parameter, start, tells splice where to start changing the array, and the second, deleteCount, indicates the number of elements in the array to remove from start? (Paraphrased from MDN Web Docs.)
start is where splice will make it work (add/replace) as you said
deletecount is the number of how many element will be deleted so if start was 1 and deletecount was 5 then
it will delete element on index 1 2 3 4 5 of the array
if use 0 (by the way any negtive number will do the same ) it mean delete 0 element which mean do not delete so any item will be added to the array
i used delete to be 1 which mean delete one element and start was i that mean delete 1 element start from i which mean delete i
if i did not provide item then it will just delete but if i provide item it will insert this item start from this start (i)
since i used single item then it will insert just one element at start index which is i
extra case
if i provided more items then it will add more items at index start
so let us take example in case if we provide more item
array has 5 item
splice(2,1,“hi”,“hello”)
that will go to index 2 then delete 1 element which will be the element on index 2 then add element hi and hello starting from index then what one index 3 will be moved to index 4 and so on so the array will have 6 item not 5 now
so you can say in this case we insert element inside the array and move the other element based on that
so splice can
add element if deletecount is 0 or negative or even removed
2)replace element if deletecount is 1 or more
modify which is not actually modify but it delete and add process in same time by read the element then do the modification then use the step 2 to do the replace
please notice item you can provide value or function which calculate the item as i did used the + operation to do the concatenation so if you have custom object you can use your custom method
and item still is parameter that why the splice has to call the concatenation or your custom method before call the splice one
so try to play it with each part and notice it behavior
not sure if i explained it in good way or not but let me know if i can do better
Thorough explanation!
Thank you for taking the time to write that out. I’ll be thinking over that for a while, but definitely got the basic concept of delete/add moving the items along down the array.