have json object of which I would like to get the max value of showstopinfo :
var data = [{
"info": [
{
"ticketinfooo": {
"showstopinfo": 1
}
},
{
"ticketinfooo": {
"showstopinfo": 6
}
}
]
}, {
"info": [
{
"ticketinfooo": {
"showstopinfo": 22
}
},
{
"ticketinfooo": {
"showstopinfo": 23
}
}
]
}]
I`ve just written the code below but it return NAN value:
var max = Math.max(...data.map(e => e.info.map(x => x.ticketinfooo.stoppinfoo.showstopinfo)))
console.log(max)
What is wrong with this code?
2
Answers
You need to
map()
on the outer array and also the innerinfo
array. From there you’ll have a 2d array which you’ll need to flatten.Here’s a working example:
You have to add
.flat()
at the end of your array. Alsox.ticketinfooo.stoppinfoo.showstopinfo
should bex.ticketinfooo.showstopinfo
.