I wasn't able to find an exact solution or if it is even possible.
I have an example array and I wont be using the first two values, and I wanted to transform those strings to numbers with Number()
and using the spread operator to convert each value in one go.
let array = ['1','2','3','4','5','6']
let result = Number(...array.splice(2));
the result of this is 3 is there a way to use the spread operator? or the only way out of this is to use map.
The expected result is result = [3,4,5,6]
so only numbers. The array of strings is just an example, it might have more strings inside or less.
The Number constructor converts one value to a number - Number(value)
You're putting in as value a spreaded array from which the first value is taken and converted
Number(...array.splice(2)) // Number('3','4','5','6') => 3
I don't see any good example using the spread operator, since you don't want to get rid of the array structure, but have to iterate the array for converting each element from string to number
let array = ['1','2','3','4','5','6']
let numbers1 = array.slice(2).map(str => +str) // [3, 4, 5, 6]
let numbers2 = array.slice(2).map(str => Number(str)) // [3, 4, 5, 6]