Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

205
Visualizações
Use Number to convert string to numbers in a copy of an array after using slice and spread operator

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.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

One possible solution is slice the array (to remove the two first elements) and then map, transform the numbers from string to number:

let array = ['1','2','3','4','5','6']
let result = array.slice(2, array.length)
let numbers = result.map(el=>Number(el))
console.log(numbers)
about 4 years ago · Juan Pablo Isaza Relatório

0

You can choose to use splice, but you don't want the result. You just want to run that to remove the first 2 values. Then map the remaining values to an array of numbers and you're good.

let array = ['1','2','3','4','5','6'];
array.splice(0, 2); // splice will remove indexes 0 and 1
let result = array.map(n => +(n)); // map the remaining values to numbers
console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

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

  1. For getting rid of the first two entries without altering the original array use .slice() The second argument can be omitted if you want to slice from the index to the end of the array
  2. To keep it short you can directly map on the spliced array using either the Number(v) constructor or by simply adding the + at the front
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]

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda