I have a collection that has fields “_dateCreated” with is a date and time field, obviously, and “amount” which is a number field.
I want to be able to populate an array/table with the date and sum of amount for each date. Is this possible? How?
I’m hoping to create an array that looks something like this:
0: “{"_createdDate":"2018-12-26","total":2}”
1: “{"_createdDate":"2018-12-27","total":1}”
2: “{"_createdDate":"2018-12-29","total":4}”
etc.,
Here’s my current code, which is creating a line for each date and time instead of for each individual date…
wixData.query(“dataset”)
.limit(1000) // include a limit if you have more than 50 items
.find()
.then((result) => {
const days = result.items.map(x => x._createdDate)
.filter((obj, index, self) => index === self.indexOf(obj))
const aggregated = days.map(x => {
return {
_createdDate: x,
total: result.items.filter(obj => obj._createdDate === x)
.map(z => z.amount)
.reduce((sum, current) => sum + current)
};
});
console.log(aggregated);
$w(“#table1”).rows = aggregated;
I’m pretty new to wix code but learning fast however this problem is so far beyond me! Any help would be greatly appreciated!