You want to use slice() (MDN docu) and not splice() (MDN docu)!
ArrayB = ArrayA.slice(0);
slice() leaves the original array untouched and just creates a copy.
splice() on the other hand just modifies the original array by inserting or deleting elements.
You want to use slice() (MDN docu) and not splice() (MDN docu)!
ArrayB = ArrayA.slice(0);
slice() leaves the original array untouched and just creates a copy.
splice() on the other hand just modifies the original array by inserting or deleting elements.
splice(0) grabs all the items from 0 onwards (i.e. until the last one, i.e. all of them), removes them from the original array and returns them.
Pretty much what the title says. My brain is a bit fried rn, but I believe I could only figure out how to empty a reactive array (i.e. const array = reactive([]);) by doing array.splice(0, array.length);. Is this the way you have to do it? Or is there other ways?
I think you want splice(0, 1).
The second argument is how many you want removed...
An integer indicating the number of old array elements to remove. If howMany is 0, no elements are removed.
Source.
Splice can work in two modes; to remove or insert items.
When removing items you'll specify two parameters: splice(index, length) where index is the starting index, and length is a positive number of elements to remove (fyi: passing a "0", as in your example, does nothing--it's saying "remove zero items starting at index"). In your case you'll want:
invalidElement.splice(indexValue, 1); // Remove 1 element starting at indexValue
When inserting items you'll specify (at least) three parameters: splice(index, length, newElement, *additionalNewElements*). In this overload you normally pass 0 as a 2nd parameter, meaning to insert the new elements between existing elements.
var invalidElements = ["Invalid2", "Invalid3"];
invalidElements = invalidElements.splice(0, 0, "Invalid1");