Most efficient way to get average by object property (array of Objects)

Siddharth Agrawal

I have user ratings for multiple courses for each user. I need to get the average user rating for each courseCode.

What would be the most efficient way to get these averages? The data can be found in either of the following two formats (though preferably the second one is better).

I was thinking of sorting the array and then aggregating values till I find a different courseCode, but I am not very good with array functions and I was wondering if there was a faster way to do this using hashmaps, .map, sets, reduce. Please help me find an efficient solution as there are many users and I would want this to be as fast as possible so that the website loads quicker.

[
    [
        {courseCode: "SYD393", rating: 3},
        {rating: 3, courseCode: "STA244"},
        {courseCode: "STA255", rating: 5},
        {rating: 5, courseCode: "CSE201"},
        {courseCode: "CSE255", rating: 4},
        {rating: 2, courseCode: "CSE202"},
        {courseCode: "ASD323", rating: 5},
    ],
    [
        {courseCode: "ASD323", rating: 5},
        {rating: 5, courseCode: "STA244"},
        {courseCode: "STA255", rating: 5},
        {courseCode: "SYD393", rating: 1},
    ],
    //...more arrays for each user
];
[
    [
        {SYD393: 3},
        {STA244: 4},
        {STA255: 5},
        {CSE255: 4},
        {ASD323: 5},
    ],
    [
        {ASD323: 5},
        {STA255: 5},
        {SYD393: 1},
    ],
    //...more arrays for each user
];
pilchard

Looks like this is similar to the answer you posted yourself, except I separated out the averaging of the Map into its own step.

const data = [[{ SYD393: 3 }, { STA244: 4 }, { STA255: 5 }, { CSE255: 4 }, { ASD323: 5 },], [{ ASD323: 5 }, { STA255: 5 }, { SYD393: 1 },],];
const avg = a => a.reduce((a, b) => a + b, 0) / a.length,

  sums = data.flat().reduce((a, o) => {
    const [[c, r]] = Object.entries(o);
    a.set(c, (a.get(c) ?? []).concat(r));
    return a;
  }, new Map),

  avgs = new Map([...sums.entries()].map(([c, rs]) => [c, avg(rs)])),

  res = data.map(us => us.map(o => {
    const [k] = Object.keys(o); return { [k]: avgs.get(k) }
  }));

console.log(res);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Though it is more straightforward using your first data shape as there is no need to wrangle the Object.entries/keys.

const data = [[{ courseCode: "SYD393", rating: 3 }, { rating: 3, courseCode: "STA244" }, { courseCode: "STA255", rating: 5 }, { rating: 5, courseCode: "CSE201" }, { courseCode: "CSE255", rating: 4 }, { rating: 2, courseCode: "CSE202" }, { courseCode: "ASD323", rating: 5 },], [{ courseCode: "ASD323", rating: 5 }, { rating: 5, courseCode: "STA244" }, { courseCode: "STA255", rating: 5 }, { courseCode: "SYD393", rating: 1 },],];
const avg = a => a.reduce((a, b) => a + b, 0) / a.length,

  sums = data.flat().reduce((a, { courseCode: c, rating: r }) =>
    (a.set(c, (a.get(c) ?? []).concat(r)), a), new Map),

  avgs = new Map([...sums.entries()].map(([c, rs]) => [c, avg(rs)])),

  res = data.map(us => us.map(({ courseCode: c }) => ({ courseCode: c, rating: avgs.get(c) })));

console.log(res);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Most efficient way to update an object property within an array of objects

Efficient way to get unique objects from array of objects, by object property?

Swift: Most efficient way to split array of objects into multiple arrays based on object property?

What is the most efficient way of accessing a property from an array of objects in PowerShell?

What is the most efficient way to convert an array of objects to an object

Restructuring JavaScript array of objects in the most efficient way

Most efficient way to merge lists of objects based on max value of object's property

JavaScript Most Efficient Way to find Object in Array

Most efficient way to search array of object values

What would be the most efficient way to find an object in an array of objects with distinct value which is an array?

Efficient way to swap property values in array of objects

Most efficient way to use an array of objects to query more objects - MongoDb?

Most efficient way to link an object in one array with an object in another array

Most efficient way to render object of lists, containing objects

Js: What is the most efficient way to sort an array of objects into sections?

Most efficient way to grab objects without any duplicates in an array

What is the most efficient way to convert JSON with multiple objects to array in JavaScript

Most efficient way to remove these objects

Is there an efficient way to get an array of objects from another array of objects

An efficient way of removing objects from an indexedDB object store that are missing a property

Most efficient way to get a column range into a 1D array

Efficient way to get indices of most common element in a numpy array

Most efficient way to get an array of every minute in a given day?

Most efficient way (if exists) to get array of files in root

Efficient way of alternating sort of array of objects based on property

Most efficient way filtering an array

Most efficient way to remove objects from array based on key value objects in another array

Most computationally efficient way to get average of particular pairs of rows, and concatenate all of the results with a particular row

Javascript Average of Array of Objects property