Ad

How Can I Use Lodash Max Against A JQuery Array?

- 1 answer

I'm trying to get the max rows out of two tables. What am I doing wrong? maxRows ends up being a tbody jQuery element and not the max property value. Any ideas? I've tried the pluck syntax as well as the long form and both seem to return the element and not the max value. See Lodash max

var tables = $('.ui-datepicker-group table tbody');
var maxRows = _.max(tables, 'childElementCount');

Thanks for any tips or tricks here. I'm obviously doing something wrong.

Ad

Answer

If you want the tbody element with the maximum number of tr's then

var tables = $('.ui-datepicker-group table tbody');
var maxRows = _.max(tables, function(el){
    return $(el).children().length
});

Demo: Fiddle

If you want the number or rows then

var tables = $('.ui-datepicker-group table tbody');
var maxRows = _.max(tables.map(function(_, el){
    return $(el).children().length
}));
console.log(maxRows)

Demo: Fiddle

Ad
source: stackoverflow.com
Ad