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

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

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 Report

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 Report

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