Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

76
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!