const transactions = [{
date: '25.01.2015',
price: '28',
},
{
date: '22.02.2015',
price: '23',
},
{
date: '24.02.2015',
price: '12',
},
{
date: '02.03.2015',
price: '55',
},
{
date: '10.03.2015',
price: '56',
},
{
date: '15.03.2015',
price: '8',
},
{
date: '24.03.2015',
price: '19',
},
{
date: '26.03.2015',
price: '12',
},
{
date: '27.03.2015',
price: '11',
},
];
function getDate(str) {
const dataTokens = str.split('.');
const date = new Date(+dataTokens[2], +dataTokens[1] - 1, +dataTokens[0]);
return date;
}
for (let i = 0; i < transactions.length; i++) {
const date = getDate(transactions[i].date);
...
}
{
const transactions = [
{
date: '25.01.2015',
price: '28',
},
{
date: '22.02.2015',
price: '23',
},
{
date: '24.02.2015',
price: '12',
},
{
date: '02.03.2015',
price: '55',
},
{
date: '10.03.2015',
price: '56',
},
{
date: '15.03.2015',
price: '8',
},
{
date: '24.03.2015',
price: '19',
},
{
date: '26.03.2015',
price: '12',
},
{
date: '27.03.2015',
price: '11',
},
];
const firstMonthDate = 25;
const firstTransaction = transactions[0].date.split('.').map((n) => +n);
const increaseMonth = (date) => {
date.month = ((date.month + 12) % 12) + 1;
if (date.month === 1) {
date.year++;
}
};
const startDate =
firstTransaction[0] < firstMonthDate
? {
day: firstMonthDate,
month: ((firstTransaction[1] + 10) % 12) + 1,
year: firstTransaction[2] - (firstTransaction[1] === 1 ? 1 : 0),
}
: {
day: firstMonthDate,
month: firstTransaction[1],
year: firstTransaction[2],
};
const endDate = { ...startDate };
increaseMonth(endDate);
endDate.day--;
const sumByMonths = [
{
from: { ...startDate },
to: { ...endDate },
sum: 0,
},
];
let sumByMonthIndex = 0;
transactions.forEach((transaction) => {
const [day, month] = transaction.date.split('.').map((n) => +n);
if (
!(month === startDate.month && day >= startDate.day) &&
!(month === endDate.month && day <= endDate.day)
) {
increaseMonth(startDate);
increaseMonth(endDate);
sumByMonths.push({
from: { ...startDate },
to: { ...endDate },
sum: 0,
});
sumByMonthIndex++;
}
sumByMonths[sumByMonthIndex].sum += +transaction.price;
});
console.log('>>> sumByMonths', sumByMonths);
}
{
const transactions = [
{
date: '25.01.2015',
price: '28',
},
{
date: '22.02.2015',
price: '23',
},
{
date: '24.02.2015',
price: '12',
},
{
date: '02.03.2015',
price: '55',
},
{
date: '10.03.2015',
price: '56',
},
{
date: '15.03.2015',
price: '8',
},
{
date: '24.03.2015',
price: '19',
},
{
date: '26.03.2015',
price: '12',
},
{
date: '27.03.2015',
price: '11',
},
];
const firstMonthDate = 25;
const firstTransaction = transactions[0].date.split('.').map((n) => +n);
const increaseMonth = (date) => {
date.month = ((date.month + 12) % 12) + 1;
if (date.month === 1) {
date.year++;
}
};
const dateToString = (date) => `${date.year}-${date.month}-${date.day}`;
const startDate =
firstTransaction[0] < firstMonthDate
? {
day: firstMonthDate,
month: ((firstTransaction[1] + 10) % 12) + 1,
year: firstTransaction[2] - (firstTransaction[1] === 1 ? 1 : 0),
unix: 0,
}
: {
day: firstMonthDate,
month: firstTransaction[1],
year: firstTransaction[2],
unix: 0,
};
startDate.unix = +new Date(dateToString(startDate));
const endDate = { ...startDate };
increaseMonth(endDate);
endDate.unix = +new Date(dateToString(endDate)) - 1;
const sumByMonths = [
{
from: {
day: startDate.day,
month: startDate.month,
year: startDate.year,
},
to: { day: endDate.day - 1, month: endDate.month, year: endDate.year },
sum: 0,
},
];
let sumByMonthIndex = 0;
transactions.forEach((transaction) => {
const date = +new Date(transaction.date.split('.').reverse().join('-'));
if (!(date >= startDate.unix && date <= endDate.unix)) {
increaseMonth(startDate);
startDate.unix = +new Date(dateToString(startDate));
increaseMonth(endDate);
endDate.unix = +new Date(dateToString(endDate)) - 1;
sumByMonths.push({
from: {
day: startDate.day,
month: startDate.month,
year: startDate.year,
},
to: { day: endDate.day - 1, month: endDate.month, year: endDate.year },
sum: 0,
});
sumByMonthIndex++;
}
sumByMonths[sumByMonthIndex].sum += +transaction.price;
});
console.log('>>> sumByMonths', sumByMonths);
}
const filteredArray = transactions.filter((item) => {
const dataTokens = item.date.split('.');
const date = new Date(+dataTokens[2], +dataTokens[1] - 1, +dataTokens[0]);
return date.getDate() >= 24 && date.getDate() <= 25;
});
console.log(filteredArray);
let currentMonth = 0;
let isFirstRun = true;
let bufferSumInMonth = 0;
const pricesArray = [];
for (let i = 0; i < transactions.length; i++) {
const currentItem = transactions[i];
const dataTokens = currentItem.date.split('.');
const date = new Date(+dataTokens[2], +dataTokens[1] - 1, +dataTokens[0]);
if (isFirstRun) { currentMonth = date.getMonth(); isFirstRun = false; }
if (currentMonth === date.getMonth() && (date.getDate() >= 24 && date.getDate() <= 25)) {
bufferSumInMonth += parseInt(currentItem.price, 10);
} else if (currentMonth !== date.getMonth() || i === transactions.length - 1) {
console.log(bufferSumInMonth);
pricesArray.push({ sum: bufferSumInMonth, month: date.getMonth() });
bufferSumInMonth = 0;
}
currentMonth = date.getMonth();
}
console.log(pricesArray);