It appears there may be a misunderstanding regarding the functionality of the splice method.
- Splice() both adds and removes elements from an array.
- Additionally, splice() replaces the original array with the modified version.
Source
When using this.selectedRoles.splice(0,1), the returned value is the element removed, not what remains in the array. This leads to the unexpected result of 1 being returned. Furthermore, by assigning it back to the same variable, the old array is replaced with the edited values.
Return Value of Splice
The deleted elements are returned in an array format.
If only one element is removed, a single-element array is returned.
If no elements are removed, an empty array is returned.
Source
If the goal is to remove the first index when the array length exceeds 1, using slice instead of splice might be more suitable. Slice does not modify the original array; instead, it creates a shallow copy of the array's elements. More details can be found here.