9

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?

RenaudC5
  • 3,553
  • 1
  • 11
  • 29
Nitin
  • 881
  • 2
  • 10
  • 37
  • Possible duplicate of [Compare JavaScript Array of Objects to Get Min / Max](http://stackoverflow.com/questions/8864430/compare-javascript-array-of-objects-to-get-min-max) – Kapila Perera Mar 28 '17 at 07:18

5 Answers5

12

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);
abhishekkannojia
  • 2,796
  • 23
  • 32
6

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);
Weedoze
  • 13,683
  • 1
  • 33
  • 63
1

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.

Vineet Markan
  • 63
  • 1
  • 10
0

You can check for the object with the lowest id by looping through the array like so, probably not the neatest way, but a solution either way:

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 tmp = list[0].id;

list.forEach(function (entry) {
    if (entry.id < tmp) {
    tmp = entry;
  }
});

console.log(tmp);

https://jsfiddle.net/camgssk3/

Dax
  • 203
  • 2
  • 7
  • there seems to be a typo here, when you initialize tmp var you are pointing to the id not the entity, suggested fix: `var tmp = list[0]; list.forEach(function (entry) { if (entry.id < tmp.id) { tmp = entry; } }); console.log(tmp);` – Kevin K. Apr 27 '20 at 11:03
0

other option may be to filter your array as you already have min "id"

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 min = Math.min.apply(Math, list.map(function (o) {
                return o.id;
            }));
filtered = list.filter(function(elem){
  if(elem.id == min){console.log(elem);return elem;}
})
Kudakv
  • 1
  • 1
  • The question already mentioned filtering the original array based on the result of `Math.min()`, and explicitly asked for a more direct method. – nnnnnn Mar 28 '17 at 07:44