Get min-max numbers from multidimensional 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
Without jquery, using this answer to add max and min to arrays:
Array.prototype.max = function() {
return Math.max.apply(null, this);
};
Array.prototype.min = function() {
return Math.min.apply(null, this);
};
The answer becomes:
arr = [[1, 5, 8, 9], [3, 7], [3, 8, 33], [2], [0, 6]]
maxm = arr.map(function(a){return a.max()}).max();
minm = arr.map(function(a){return a.min()}).min();