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

75
Vistas
How to find the lightest object

How can I find the lightest object from array of objects? I do not want to get value, but the object with this value. Not for instance just 1200 but the object {mass: 1200, numberOfel: ..., indexOfEl: ... , isCorrect: boolean}

let massOfElephants = [2400, 2000, 1200, 2400, 1600, 4000];
let startOrder = [1, 4, 5, 3, 6, 2];
const elephantObject = [];
  
  startOrder.forEach((val, i) => {
    elephantObject.push({
      mass: massOfElephants[val - 1],
      numberOfEl: val,
      indexOfEl: i,
      isCorrect: false,
    });
  });
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Since you have a list of objects already, all you need to do is sort the objects according to their mass properties. The way to go about it is to write a custom comparator function which to sort by (See Array.prototype.sort()).

For numeric types, the comparator function (a,b) => a - b will sort the values increasingly, and myArray.sort((a, b) => a - b) will sort myArray accordingly.

After sorting elephantObject.sort((a,b) => a.mass - b.mass), the lightest elephant will be the first entry of your object array: elephantObject[0]. as

about 4 years ago · Juan Pablo Isaza Denunciar

0

The reduce method reduces an array to a single value using a callback function.

const getLightest = (initial, current){
    return (initial.mass < current.mass)?initial:current;
}
const lightest = elephantObject.reduce(getLightest);
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