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

116
Vistas
Javascript get object from array having min value and min value is needs to find and then find object

I have array of objects in object have different key so i want the object having minimum value in that array

list = [
{
'id':4,
'name':'nitin',
'group':'angularjs'
},
{
'id':1,
'name':'nitin',
'group':'angularjs'
},
{
'id':2,
'name':'nitin',
'group':'angularjs'
},
{
'id':3,
'name':'nitin',
'group':'angularjs'
}]

I tried by

var minValue = Math.min.apply(Math, list.map(function (o) {
                return o.id;
            }))

but it returns only the id not whole object then i have to make more filter for get object

is there any direct method to find object?

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can use Array reduce method:

var list = [
{
'id':4,
'name':'nitin',
'group':'angularjs'
},
{
'id':1,
'name':'nitin',
'group':'angularjs'
},
{
'id':2,
'name':'nitin',
'group':'angularjs'
},
{
'id':3,
'name':'nitin',
'group':'angularjs'
}];

var result = list.reduce(function(res, obj) {
    return (obj.id < res.id) ? obj : res;
});

console.log(result);

about 4 years ago · Santiago Trujillo Denunciar

0

Using Array#reduce()

list = [{
    'id': 4,
    'name': 'nitin',
    'group': 'angularjs'
  },
  {
    'id': 1,
    'name': 'nitin',
    'group': 'angularjs'
  },
  {
    'id': 2,
    'name': 'nitin',
    'group': 'angularjs'
  },
  {
    'id': 3,
    'name': 'nitin',
    'group': 'angularjs'
  }
];

let min = list.reduce((prev, curr) => prev.id < curr.id ? prev : curr);

console.log(min);

about 4 years ago · Santiago Trujillo Denunciar

0

I tried these solutions with Array.reduce but none of them were full proof. They do not handle cases where the array is empty or only has 1 element.

Instead this worked for me:

const result = array.reduce(
    (prev, current) =>
      (prev?.id ?? current?.id) >= current?.id ? current : prev,
    null,
  );

It returns null if array is empty and handles other cases well.

about 4 years ago · Santiago Trujillo 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