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

293
Visualizações
Return an array with only first 3 character/numbers from each item in an arrays

How can I return only the first 3 characters/digits from each item in an array?

For example: The string 123456 should return 123, 987654 should return 987, and so on.

Given an array of:

['7177576', '4672769', '2445142', '9293878', '5764392']

Expected return:

['717', '467','244', '929', '576']

I have tried .slice() but that returns the first three like this:

['7177576', '4672769', '2445142'].
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

slice works for strings, too.

 const
     data = ['7177576', '4672769', '2445142', '9293878', '5764392'],
     result = data.map(string => string.slice(0, 3));

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

JavaScript has many methods for manipulating arrays. The map method creates a new array populated with the results of calling a provided function on every element in the calling array. As Nina Scholz mentions above you can use slice on strings as well.

The issue you are having is that you are calling slice on the array and not on the items of the array.

You need to do something like Nina Suggest which is to map over the main array and call your slice function for each item.

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