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

178
Vistas
When using using reduce with ternary inside a map, I seem to have troubles

this is to return an array of numbers that are the highest value of the arrays inside of the base array. I can get it to work when i use for statements. But I have tried to simplify it, and can't work out why it doesn't work. any help would be appriciated.

    function largestOfFour(arr) {
      return arr.map((x) => x.reduce((a, c) =>  c > a ? c : a, 0));
    }

Example with input and output:

const input = [
  [1,2,3,4,5],
  [6,5,4,3,2,1],
  [1,7,3,4,5,6],
];

function findHighestElements(arr) {
    return arr.map((x) => x.reduce((a, c) =>  c > a ? c : a, 0));
}

console.log(findHighestElements(input)); // [5,6,7]

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

0

You don't need to reduce, you can do it by just Math.max. like this:

function findMaxNumbers(arr) {
  return arr.map((x) => Math.max(...x));
}

let test = [[1, 2, 3],[4, 5, 6],[7, 8, 9]];
console.log(findMaxNumbers(test));

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you have values smaller than zero, you need to remove the start value

x.reduce((a, c) =>  c > a ? c : a, 0)
                                   ^

or use a very small start value, like -Number.MAX_VALUE.

about 4 years ago · Juan Pablo Isaza Denunciar

0

To get the max of all the maxes, you can reduce the reductions. If you just want the max's, map the reduction.

const maxOfArray = a => a.reduce((a, c) => c > a ? c : a, -Number.MAX_SAFE_INTEGER); // thanks to Nina
const conciseVLAZMax = a => Math.max(...a); // thanks to VLAZ 

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

let maxs = a.map(maxOfArray);
let maxMax = maxOfArray(maxs);

console.log(maxs);
console.log(maxMax);

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