I have an array of objects like so:
let data = [
{date:'2020-05-01', device: 'iphone', site: 'google', val1:10, val2:20, val3:30},
{date:'2020-05-01', device: 'iphone', site: 'bing', val1:23, val2:12, val3:14},
{date:'2020-05-01', device: 'iphone', site: 'jeeves', val1:67, val2:78, val3:12},
{date:'2020-05-03', device: 'iphone', site: 'google', val1:10, val2:20, val3:30},
{date:'2020-05-03', device: 'iphone', site: 'bing', val1:23, val2:12, val3:14},
{date:'2020-05-03', device: 'iphone', site: 'jeeves', val1:67, val2:78, val3:12}
];
I want to subtract the vals from 2020-05-03 with 2020-05-01when the site is the same and finally return something like:
[
{device: 'iphone', site: 'google', val1:-5, val2:5, val3:15},
{device: 'iphone', site: 'bing', val1:10, val2:-10, val3:53},
{device: 'iphone', site: 'jeeves', val1:10, val2:21, val3:13},
]
I tried to do a forEach but I am not really sure where to go from there and how to write a function to make this more reusable:
data.forEach(d => {
if (d.data === )
});
Sorry, I really don't where to go from here which is why i really cant show what I tried.