I'm currently working with data fetched from an API response in an attempt to identify the pitch with the highest speed. Below is a snippet from the API response.
{
page: 1,
total_pages: 4,
listings: [
{
name: "Zack Greinke",
pitches: [
{
name: "Slider",
speed: 88,
control: 79,
},
{
name: "Curveball",
speed: 77,
control: 67,
},
{
name: "Fastball",
speed: 95,
control: 82,
}
]
},
{
name: "Max Scherzer",
pitches: [
{
name: "Changeup",
speed: 84,
control: 76,
},
{
name: "Sinker",
speed: 92,
control: 80,
}
]
},
]
}
Here's my approach:
itemSet.forEach( (item) => {
let fastestPitch = Object.keys(item.pitches).reduce((a, b) => {
item.pitches[a] > item.pitches[b] ? item.pitches[a].name : item.pitches[b].name
});
});
However, this method consistently returns the name of the last pitch listed. My goal is to fetch the pitch with the highest speed.
Update: I also attempted the following solution, but encountered an error message.
itemSet.forEach( (item) => {
let fastestPitch = Object.keys(item.pitches).reduce((a, b) => {
item.pitches[a].speed > item.pitches[b].speed ? item.pitches[a].name : item.pitches[b].name
});
});
Error Encountered:
(node:80698) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'speed' of undefined