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

140
Vistas
Creating new array based on another array values - Typescript

I have an array of weekday indices (consisting of the numbers 0-6) that need to be converted to the appropriate corresponding 3-letter weekday strings.

For instance, the array [0, 1, 2, 3, 4, 5, 6] should be converted to ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"], and [2,4] should be converted to ["Wed","Fri"]. This is my given input data:

 "data":[
        {
           "name":"John",
           "gender":"M",
           "age":"40",
           "days_present":[0,1,2,3,4,5,6],
           "time_spend":1626908399000
        },
     {
           "name":"Maria",
           "gender":"F",
           "age":"32",
           "days_present":[2,6],
           "time_spend":1626908366000
        },
      {
           "name":"Ben",
           "gender":"M",
           "age":"27",
           "days_present":[2,4,6],
           "time_spend":1626908331000
        }
  ]

And the resultant data should appear as follows:

  "data":[
        {
           "name":"John",
           "gender":"M",
           "age":"40",
           "days_present":["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
           "time_spend":1626908399000
        },
     {
           "name":"Maria",
           "gender":"F",
           "age":"32",
           "days_present":["Wed","Sun"],
           "time_spend":1626908366000
        },
      {
           "name":"Ben",
           "gender":"M",
           "age":"27",
           "days_present":["Wed", "Fri","Sun"],
           "time_spend":1626908331000
        }
  ]

How should I best go about doing this?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Pretty simple, just do two map calls:

const obj={"data":[{"name":"John","gender":"M","age":"40","days_present":[0,1,2,3,4,5,6],"time_spend":1626908399000},{"name":"Maria","gender":"F","age":"32","days_present":[2,6],"time_spend":1626908366000},{"name":"Ben","gender":"M","age":"27","days_present":[2,4,6],"time_spend":1626908331000}]};

const weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];

const result = obj.data.map(({ days_present, ...r }) => ({
  days_present: days_present.map(i => weekdays[i]),
  ...r
}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: auto; }

This loops over each item in data and converts each of the weekday indices to their corresponding weekday string.

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