I came across this topic: Merge multiple array of objects and sum duplicate values javascript, and it was so close to something I was trying to do.
I essentially need the same thing, but I need the average of the values instead of the sum. What I thought would do the trick is:
var array1 = [{
price: "2",
ref: "A"
},
{
price: "20",
ref: "B"
},
{
price: "23",
ref: "C"
},
{
price: "23",
ref: "D"
}
];
var array2 = [{
price: "12",
ref: "A"
},
{
price: "5",
ref: "B"
},
{
price: "23",
ref: "E"
},
{
price: "23",
ref: "F"
}
];
var array3 = [{
name: "Blah",
fcp: "erol"
},
{
name: "Blah2",
fcp: "tpep"
}
];
var averagedArr = 2
var result = array1.concat(array2, array3).reduce(function(acc, curr) {
if (curr.ref) {
var fromMap = acc.map[curr.ref];
if (!fromMap) {
acc.map[curr.ref] = fromMap = {
price: 0,
ref: curr.ref
}
acc.result.push(fromMap);
}
fromMap.price += parseFloat(curr.price);
fromMap.price = parseFloat(fromMap.price/averagedArr);
} else {
acc.result.push(curr);
}
return acc;
}, {
map: {},
result: []
}).result;
console.log(result);
It isn't working because it seems to be averaging each price as it is added and not after they have been added all together. For example instead of for ref:B the result being 12.5 (20+5 divided by 2) the result is 7.5 (20/2=10; 10+5=15; 15/2 = 7.5). If anyone could help me figure this out I would be most appreciative.
I would have just commented on the original topic to @Grundy who provided the response I was using, but this is my first question and I can't comment.
Here is the fiddle. https://jsfiddle.net/5xqbk9Lg/
Thanks in advance!