There are many ways of doing it. The first one is to reduce the array maintaining the max/min value:
let tab= [2];
tab[0] = [{name :"book", value : 8}, {name :"cake", value : 2}]
tab[1] = [{name :"apple", value : 1}, {name :"spaceship", value : 200}]
console.log(
tab.map(a=>a.reduce((a,b)=>a.value>b.value?a:b,{})).reduce((a,b)=>a.value>b.value?a:b,{})
)
This can be easier if we flatten the array as follow
let tab= [2];
tab[0] = [{name :"book", value : 8}, {name :"cake", value : 2}]
tab[1] = [{name :"apple", value : 1}, {name :"spaceship", value : 200}]
console.log(
[].concat.apply([], tab).reduce((a,b)=>a.value>b.value?a:b,{})
)
Those above functions get the max value, to get the min simply change a.value>b.value
to a.value<b.value
. Also, those functions has a time complexity of O(n)
so with larger arrays it will work faster.
The other way is to sort and get the first/last value
let tab= [2];
tab[0] = [{name :"book", value : 8}, {name :"cake", value : 2}]
tab[1] = [{name :"apple", value : 1}, {name :"spaceship", value : 200}]
console.log(
[].concat.apply([], tab).sort((a,b)=>b.value-a.value)[0]
)
However this method is more complex as it sorts the array first (O(n log n)) and then gets the first or last element (O(1)).