I am getting a JSON file which contains weather data about a number of cities. Here is an example of the data.
const weatherArray = [{
"Date": '5-1-19',
"Location": 'New York',
"Rainfall": 4},
{
"Date": '5-1-19',
"Location": 'Miami',
"Rainfall": 8},
{
"Date": '5-1-20',
"Location": 'New York',
"Rainfall": 9},
{
"Date": '5-1-20',
"Location": 'Miami',
"Rainfall": 2},
{
"Date": '5-1-21',
"Location": 'New York',
"Rainfall": 10},
{
"Date": '5-1-21',
"Location": 'Chicago',
"Rainfall": 9},
]
What I need to do is filter this data and store the maximum rainfall for each city in an array. I believe my function is close but filterData is returning an array of 6 unknown objects.
filterData = (inputArray) => {
let rain = inputArray.map(obj => rain.find(o => o.Location === obj.Location && obj.Rainfall > o.Rainfall) || rain.find(o => o.Location !== obj.Location));
return rain;
}
I would like the output array to contain the entire object associated with the maximum rainfall for each city in the JSON file.
rain = [{
"Date": '5-1-19',
"Location": 'Miami',
"Rainfall": 8},
{
"Date": '5-1-21',
"Location": 'New York',
"Rainfall": 10},
{
"Date": '5-1-21',
"Location": 'Chicago',
"Rainfall": 9},
]