简体   繁体   中英

Find object having maximum value for the `id` property in an array of objects

In my array of objects, I want to find the object with the highest value for the id property.

Here is my array:

myArray = [
  {
    'id': '73',
    'foo': 'bar'
  },
  {
    'id': '45',
    'foo': 'bar'
  },
  // …
];

Generally, I use $.grep to find values in an array, like this:

var result = $.grep(myArray, function (e) {
    return e.id == 73;
});

But in this case I need to provide a specific id value for the object I want to select.

The question states that he wants to find the object with the greatest id, not just the greatest id...

var myArray = [{'id':'73','foo':'bar'},{'id':'45','foo':'bar'}];

var max = myArray.reduce(function(prev, current) {
    if (+current.id > +prev.id) {
        return current;
    } else {
        return prev;
    }
});

// max == {'id':'73','foo':'bar'}

Use the map() method of the array. Using map you can provide a function that iterates over every element in the array. In that function, you can work out the object with the highest id. For example:

myArray = [{'id':'73','foo':'bar'},{'id':'45','foo':'bar'}];

var maxid = 0;

myArray.map(function(obj){     
    if (obj.id > maxid) maxid = obj.id;    
});

This will give you the max id of the objects in the array.

Then you can use grep to get the related object:

var maxObj = $.grep(myArray, function(e){ return e.id == maxid; });

Alternatively, if you just want the object with the max id, you can do this:

var maxid = 0;
var maxobj;

myArray.map(function(obj){     
    if (obj.id > maxid) maxobj = obj;    
});

//maxobj stores the object with the max id.
const students = [
  { id: 100, name: 'Abolfazl', family: 'Roshanzamir' },
  { id: 2, name: 'Andy', family: 'Madadian' },
  { id: 1500, name: 'Kouros', family: 'Shahmir' }
]

If you want to find the object with max Id :

const item = students.reduce((prev, current) => (+prev.id > +current.id) ? prev : current)
 // it returns  { id: 1500, name: 'Kouros', family: 'Shahmir' }

If you want to find the object with min Id :

const item = students.reduce((prev, current) => (+prev.id < +current.id) ? prev : current)
// it returns {id: 2, name: "Andy", family: "Madadian"}

If you wnat to find the max Id :

const max = Math.max.apply(null, students.map(item => item.id));
// it returns 1500

If you want to find the min Id :

const min = Math.min.apply(null, students.map(item => item.id));
// it returns 2 

 function reduceBy(reducer, acc) { return function(by, arr) { return arr[arr.reduce(function(acc, v, i) { var b = by(v); return reducer(acc[0], b) ? [b, i] : acc; }, acc || [by(arr[0]), 0])[1]]; }; } var maximumBy = reduceBy(function(a,b){return a<b;}); var myArray = [{'id':'73','foo':'bar'},{'id':'45','foo':'bar'}]; console.log(maximumBy(function(x){ return parseInt(x.id,10) }, myArray)); // {'id':'73','foo':'bar'}

var max = 0;
var myArray = [{'id':'73','foo':'bar'},{'id':'45','foo':'bar'}]
var maxEle = myArray.map(function(ele){ if(ele.id>max){ max=ele} });

map is a function which iterates through array elements and performs specific operation.

let id = items.reduce((maxId, item) => Math.max(maxId, item.id), 0);

or

let id = Math.max(...items.map(item => item.id).concat(0)); // concat(0) for empty array
// slimmer and sleeker ;)
let id = Math.max(...items.map(item => item.id), 0);

This way is more practical, because in the case of an empty array, it returns 0, unlike

Math.max.apply(null, [].map(item => item.id)) // -Infinity

and if you want to get "autoincrement", you can just add 1 regardless of whether the array is empty or not

// starts at 1 if our array is empty
autoincrement = items.reduce((maxId, item) => Math.max(maxId, item.id), 0) + 1;

UPD: Code with map is shorter but with reduce is faster , which is felt with large arrays

 let items = Array(100000).fill() .map((el, _, arr) => ({id: ~~(Math.random() * arr.length), name: 'Summer'})); const n = 100; console.time('reduce test'); for (let i = 1; i < n; ++i) { let id = items.reduce((maxId, item) => Math.max(maxId, item.id), 0); } console.timeEnd('reduce test'); console.time('map test'); for (let i = 1; i < n; ++i) { let id = Math.max(items.map(item => item.id).concat(0)); } console.timeEnd('map test'); console.time('map spread test'); for (let i = 1; i < n; ++i) { let id = Math.max(...items.map(item => item.id), 0); } console.timeEnd('map spread test');

reduce test: 163.373046875ms
map test: 1282.745849609375ms
map spread test: 242.4111328125ms

If we create an even larger array, spread map will shutdown

let items = Array(200000).fill()
    .map((el, _, arr) => ({id: ~~(Math.random() * arr.length), name: 'Summer'}));

reduce test: 312.43896484375ms
map test: 2941.87109375ms
Uncaught RangeError: Maximum call stack size exceeded at :15:32

Assuming that ID's are "stringified" numbers and achieving this by only use a map:

let arr = [{'id':'73','foo':'bar'},{'id':'45','foo':'bar'}],
    maxIndex = -1,
    maxId;

arr.map(function(obj, i){  
    if(maxIndex === -1){
     maxIndex = i;
     maxId = Number(obj.id);
    } else {
     if (Number(obj.id) > maxId){
      maxId = Number(obj.id);
      maxIndex = i; 
     }
    }
});

if(maxIndex !== -1) console.log(`Selected object: ${JSON.stringify(arr[maxIndex])}`)
else console.warn('No max ID. No ID\'s at all');

使用reduce()的缩短版本

myArray.reduce((max, cur)=>(max.likes>cur.likes?max:cur))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM