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

156
Vistas
How can I map a nested array in JS

Here is my array:

const main = [
  [['02:20:21,369'], ['02:20:21,369'], ['02:20:21,369'], ['02:20:21,369']],
  [['02:20:21,369'], ['02:20:21,369']],
  [['02:20:21,369'], ['02:20:21,369'], ['02:20:21,369']],
  [['02:20:21,369']],
];

and here is my function:

const convertor = (x) => {
  const splitted = x.split(':');
  console.log(splitted);
  const converted = splitted[0] * 60 + splitted[1] * 60 + splitted[2];
  return converted;
};

I want to map this function on each nested array

I tried this but I got an error:

const resu = main.map((x) => {
  x.map((y) => {
    convertor(y);
  });
});

x.split is not a function

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

0

Issues

  1. Note that there are 3 level nested arrays, so there should be 3 maps()
  2. return is required if you use {} in the arrow function

const main = [
  [['02:20:21,369'], ['02:20:21,369'], ['02:20:21,369'], ['02:20:21,369']],
  [['02:20:21,369'], ['02:20:21,369']],
  [['02:20:21,369'], ['02:20:21,369'], ['02:20:21,369']],
  [['02:20:21,369']],
];

const convertor = (x) => {
  const splitted = x.split(':');
  const converted = splitted[0] * 60 + splitted[1] * 60 + splitted[2];
  return converted;
};

const resu = main.map((x) => {
  return x.map((y) => {
    return y.map((z) => {
      return convertor(z);
    });
  });
});

console.log(resu);

Shorter version

main.map(x => x.map(y => y.map(convertor)));
about 4 years ago · Juan Pablo Isaza Denunciar

0

const main = [
  [
    ['02:20:21,369'],
    ['02:20:21,369'],
    ['02:20:21,369'],
    ['02:20:21,369']
  ],
  [
    ['02:20:21,369'],
    ['02:20:21,369']
  ],
  [
    ['02:20:21,369'],
    ['02:20:21,369'],
    ['02:20:21,369']
  ],
  [
    ['02:20:21,369']
  ],
];


const convertor = (x) => {
  const splitted = x.split(':');
  //console.log(splitted);
  const converted = splitted[0] * 60 + splitted[1] * 60 + splitted[2];
  return converted;
};

const mappedMain = main.map(i => {
 return i.map(j => {
  return convertor(...j)
 })
})

//Or
//const mappedMain = main.map(i => i.map(j => convertor(...j)))



console.log(mappedMain);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You made some mistakes:

  1. Expression (x) => { /* a few lines of code */ } requires the use of the return keyword to return the result, while (x) => /* single line of code */ doesn't.

  2. Your array main is three-dimensional array, not two-dimensional.

Try this:

const resu = main.map((x) => {
  return x.map((y) => {
    return y.map(convertor);
  });
});

Or easier:

const resu = main.map(
    (x) => x.map(
        (y) => y.map(convertor)
    )
);

const main = [
  [['02:20:21,369'], ['02:20:21,369'], ['02:20:21,369'], ['02:20:21,369']],
  [['02:20:21,369'], ['02:20:21,369']],
  [['02:20:21,369'], ['02:20:21,369'], ['02:20:21,369']],
  [['02:20:21,369']],
];

const convertor = (x) => {
  const splitted = x.split(':');
  const converted = splitted[0] * 60 + splitted[1] * 60 + splitted[2];
  return converted;
};

const resu = main.map(
    (x) => x.map(
        (y) => y.map(convertor)
    )
);

console.log(resu);

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