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

264
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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