Here is an array of data provided:
const ratingData = [
{name: 'St. Marys School', rating:5},
{name: 'St. Zaviers School', rating:4},
{name: 'St. Marys School', rating:3},
{name: 'Rahul English Medium School', rating:2},
{name: 'St. Francis High School', rating:3},
{name: 'Rahul English Medium School', rating:1},
{name: 'St. Francis High School', rating:4},
{name: 'Mother Marys High School', rating:5}
];
My goal is to calculate the average rating for each school in this array. The challenge lies in dealing with duplicate entries, such as having multiple ratings for a single school like "St. Mary's School". How can I combine these entries and calculate the average rating effectively? I attempted using the map
function, but it did not provide the desired outcome.
The expected output should resemble the following: (This is just an example)
const output = [
{ name: 'St. Marys School', averageRating: 4},
{ name: 'St. Zaviers School', averageRating: 4},
{ name: 'Rahul English Medium School', averageRating: 1.5},
{ name: 'St. Francis High School', averageRating: 3.5},
{ name: 'Mother Marys High School', averageRating: 5}
];