If I have an array containing objects with hundreds of fields structured like the example below:
[
{
"designation":"419880 (2011 AH37)",
"discovery_date":"2011-01-07T00:00:00.000",
"h_mag":19.7,
"moid_au":0.035,
"q_au_1":0.84,
"q_au_2":4.26,
"period_yr":4.06,
"i_deg":9.65,
"pha":true,
"orbit_class":"Apollo"
}
I am attempting to find the maximum value of "h_mag" for all the data points that meet certain criteria using this function:
function filterByPHA (neowise){
for (let i = 0; i < neowise.length; i++) {
let neo = neowise[i];
if (neo.pha === true) {
console.log(`${neo.designation}: ${neo.orbit_class}`);
}
}
}
filterByPHA(neowise);
The function is functioning properly.
I have made an attempt with the following code:
const maxMOID = Math.max(...filterByPHA(neowise).map(function(x){
return x.moid_au;
}));
console.log(maxMOID);
My understanding is that this code should apply Math.max to my function filterByPHA(neowise), mapping it to a new function that returns the maximum moid value from the array within filterByPHA(neowise). However, I am encountering a 'TypeError: Cannot read properties of undefined (reading 'map')'. The 'x' in this context is simply a placeholder and I'm uncertain about what needs to be placed there to resolve this issue or if this code is even functional.