Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

294
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda