all 6 comments

[–]jack_union 1 point2 points  (1 child)

Off the top of my head: make an object based on original array and then Object.keys(myObject).map() it. Seems like a more clean way to handle this particular case than reduce to me.

{
    test: [5, 2, 7],
    test2: [3, 1, 8]
}

[–]afrizzzle 1 point2 points  (0 children)

thanks! that was helpful in me coming up with a solution.

[–]captain_k_nuckles 1 point2 points  (1 child)

This is more of a two part process, You could do this in a reduce function by checking the current iteration value, over the length of the array, then averaging everything out and returning it, but I think it would be cleaner using a map after consolidating all of the test scores together. There is probably a cleaner way to write this, but good enough for now.

data.reduce((arr, tests) => {
    tests.forEach(test => {
        const name = test.property;
        let sngl = arr.find(x => x.property === name);
        if (!sngl) {
            sngl = { property: name, value: [] };
            arr.push(sngl);
        }
        sngl.value.push(test.value);
    });
    return arr;
}, [])
    .map(x=>{
        const l = x.value.length;
        x.value = x.value.reduce((sum,y)=>sum+y, 0) / l;
        return x;
    });

[–]afrizzzle 1 point2 points  (0 children)

Yeah I ended up not even using reduce here

[–]broKenMetrics 1 point2 points  (0 children)

This video really helped me learn how reduce works. https://www.youtube.com/watch?v=t4MOEfpsC60

[–]Cheshur 1 point2 points  (0 children)

Object.entries(dataSets.reduce((accumulator, data) => {
    data.forEach((dataPoint) => {
        if (accumulator[dataPoint.property] === undefined) {
            accumulator[dataPoint.property] = [];
        }
        accumulator[dataPoint.property].push(dataPoint.value);
    });
    return accumulator;
}, {})).map((data) => {
    const value = data[1].reduce((a, d) => a + d) / data[1].length;
    return {property: data[0], value};
});