If I have an array like this:
array = [[1, 5, 8, 9], [3, 7], [3, 8, 33], [2], [0, 6]]
I need to find max and min values from this array. In this case, max = 33, min = 0
I saw examples of array reduce, but I don't want to find max value for particular index of the inner array.
Just try with:
var flat = [];
$.map(array, function(item){ $.merge(flat, item); });
// or merge arrays using `join` and `split`
var flat = array.join().split(',');
var max = Math.max.apply( Math, flat ),
min = Math.min.apply( Math, flat );
Here is pure JS based solution. Without jQuery:
var flattened = [[1, 5, 8, 9], [3, 7], [3, 8, 33], [2], [0, 6]].reduce(function(a, b) {
return a.concat(b);
});
Math.max.apply(null, flattened) //33
Math.min.apply(null, flattened) // 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With