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

116
Vistas
Concurrent Array Functions

I have a function that takes an array of sub-arrays containing number strings and returns an array with the highest number from each sub-array:

const largestOfFour = arr => {
            arr.map(e => {
                e.sort((a,b) => b - a).splice(1);
            });
            return arr.flat();
        } 

It works perfectly in the way it is formatted above but it does not work if it is formatted with concurrent functions on the array passed in. Like this:

const largestOfFour = arr => {
            return arr.map(e => {
                e.sort((a,b) => b - a).splice(1);
            }).flat();
        }

In that format it returns an array of appropriate length but each element is null. Can anyone help me understand why that is? I usually don't have a problem chaining concurrent functions onto an array.

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

0

There is no concurrency in JavaScript. But your map callback does not return anything (there is no return in the statement block) so .map() returns an array of undefined values.

To avoid that the original data gets mutated, I would also avoid the use of sort on the original array, and use slice instead of splice.

But as you only need one value from each array, you don't even need slice: just get the value at index 0 (making flat unnecessary):

let a = [
  ["1", "2", "3"],
  ["4", "5", "6"]
];

const largestOfFour = arr =>
  arr.map(e =>
    [...e].sort((a, b) => b - a)[0]
  );

console.log(largestOfFour(a));

Or even better, just use Math.max, which also makes the conversion to number:

let a = [
  ["1", "2", "3"],
  ["4", "5", "6"]
];

const largestOfFour = arr => arr.map(e => Math.max(...e));

console.log(largestOfFour(a));

about 4 years ago · Juan Pablo Isaza Denunciar

0

The inner block (in map) issn't returning anything, resulting in an array of undefined. Fixed in the snippet...

let a = [
  ["1", "2", "3"],
  ["4", "5", "6"]
]

const largestOfFour = arr => {
  return arr.map(e => {
    return e.sort((a, b) => b - a)[0];
  }).flat();
}

console.log(largestOfFour(a))

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