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

257
Views
How to find the min/max of values in an array of json objects (javascript)

I have a JSON array that looks something like this:

arr = [{"range":4, "time":56, id:"a4bd"},
{"range":5, "time":65, id:"a4bg"},
{"range":3, "time":58, id:"a4cd"},
{"range":3, "time":57, id:"a4gh"},
{"range":8, "time":60, id:"a5ab"}]

I'd like to know the best way in javascript to find the minimum or maximum value for one of these properties, eg.

min(arr["time"])

or something similar, which would return 56

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Use Math.max and Math.min with array

Reference

  • Math.min
  • Math.max

These functions will return the minimum/maximum values from a list of input numbers.

what you have to do is generate the list od numbers against which you need to find min or max. I used Array.map to return the vaues from each node of array. Spread the array to a list of numbers and apply Math.min and Math.max

const arr = [{ "range": 4, "time": 56, id: "a4bd" },
{ "range": 5, "time": 65, id: "a4bg" },
{ "range": 3, "time": 58, id: "a4cd" },
{ "range": 3, "time": 57, id: "a4gh" },
{ "range": 8, "time": 60, id: "a5ab" }];

function findMinMax(key) {
  const datas = arr.map((node) => node[key]);
  return {
    min: Math.min(...datas),
    max: Math.max(...datas),
  }
}
console.log(findMinMax('range'));
console.log(findMinMax('time'));

about 4 years ago · Juan Pablo Isaza Report

0

For example lodash solution for minimum option:

import _ from 'lodash';

const myMin = (arr, prop) => _.minBy(arr, (o) => o[prop])[prop];
       
myMin(arr, 'time');  // 56
myMin(arr, 'range'); // 3
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!