I have a JSON file containing various transactions
with both a date
and a price
attribute. My goal is to compare the dates, identify transactions that occur in the same month and year, and calculate the total price of those transactions.
JSON:
transactions: [
{
date: "2017-11-17",
price: "28",
},
{
...
}
JavaScript:
request.onload = function() {
for(const transaction of request.response.transactions) {
let year = new Date(transaction.date).getFullYear();
let month = new Date(transaction.date).getMonth();
console.log(year + ' ' + month); // output: 2017-11 ...
}
};
Although I attempted to iterate through the JSON object, I am currently struggling to devise a method for comparing the dates effectively.