Я пытаюсь получить измененный объект данных, переданный в функцию countAmountInOneWeek, но когда я пытаюсь получить его, я получаю сообщение undefined.
Это для подсчета суммы за последние 7 дней, после подсчета суммы, которую я удаляюпервые семь дней, чтобы я мог посчитать сумму за 7 дней еще раз.
[{ "date": "2016-01-05", "user_id": 1, "user_type": "natural", "type":
"cash_in", "operation": { "amount": 200.00, "currency": "EUR" } },
{ "date": "2016-01-06", "user_id": 2, "user_type": "juridical", "type":
"cash_out", "operation": { "amount": 300.00, "currency": "EUR" } },
{ "date": "2016-01-06", "user_id": 1, "user_type": "natural", "type":
"cash_out", "operation": { "amount": 30000, "currency": "EUR" } },
{ "date": "2016-01-07", "user_id": 1, "user_type": "natural", "type":
"cash_out", "operation": { "amount": 1000.00, "currency": "EUR" } },
{ "date": "2016-01-07", "user_id": 1, "user_type": "natural", "type":
"cash_out", "operation": { "amount": 100.00, "currency": "EUR" } },
{ "date": "2016-01-10", "user_id": 1, "user_type": "natural", "type":
"cash_out", "operation": { "amount": 100.00, "currency": "EUR" } },
{ "date": "2016-01-10", "user_id": 2, "user_type": "juridical", "type":
"cash_in", "operation": { "amount": 1000000.00, "currency": "EUR" } },
{ "date": "2016-01-10", "user_id": 3, "user_type": "natural", "type":
"cash_out", "operation": { "amount": 1000.00, "currency": "EUR" } },
{ "date": "2016-02-15", "user_id": 1, "user_type": "natural", "type":
"cash_out", "operation": { "amount": 300.00, "currency": "EUR" } }]
readJSON = function() {
fs.readFile(args, 'utf8', function(err, data) {
if (err) throw err;
obj = JSON.parse(data);
//console.log(obj);
printResult(obj);
var sum = countAmountInOneWeek(obj);
console.log("SUM " + sum[0]);
console.log("SUM " + sum[1].data);
});
}
Мне нужно получить измененный объект данных и сумму
function countAmountInOneWeek(data) {
var recordRemoveIndex;
var sum = data[0].operation.amount;
for (var i = 1; i < data.length; i++) {
var date1 = new Date(data[0].date);
var date2 = new Date(data[i].date);
const diffTime = Math.abs(date2.getTime() - date1.getTime());
const difference = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
//console.log(diffTime + " Time " + difference);
if (difference < 7) {
sum = sum + data[i].operation.amount;
recordRemoveIndex = i;
}
}
for (var i = 0; i <= recordRemoveIndex; i++) {
data.shift();
}
console.log(data);
return [sum, data];
}
var data = [{ "date": "2016-01-05", "user_id": 1, "user_type": "natural", "type":
"cash_in", "operation": { "amount": 200.00, "currency": "EUR" } },
{ "date": "2016-01-06", "user_id": 2, "user_type": "juridical", "type":
"cash_out", "operation": { "amount": 300.00, "currency": "EUR" } },
{ "date": "2016-01-06", "user_id": 1, "user_type": "natural", "type":
"cash_out", "operation": { "amount": 30000, "currency": "EUR" } },
{ "date": "2016-01-07", "user_id": 1, "user_type": "natural", "type":
"cash_out", "operation": { "amount": 1000.00, "currency": "EUR" } },
{ "date": "2016-01-07", "user_id": 1, "user_type": "natural", "type":
"cash_out", "operation": { "amount": 100.00, "currency": "EUR" } },
{ "date": "2016-01-10", "user_id": 1, "user_type": "natural", "type":
"cash_out", "operation": { "amount": 100.00, "currency": "EUR" } },
{ "date": "2016-01-10", "user_id": 2, "user_type": "juridical", "type":
"cash_in", "operation": { "amount": 1000000.00, "currency": "EUR" } },
{ "date": "2016-01-10", "user_id": 3, "user_type": "natural", "type":
"cash_out", "operation": { "amount": 1000.00, "currency": "EUR" } },
{ "date": "2016-02-15", "user_id": 1, "user_type": "natural", "type":
"cash_out", "operation": { "amount": 300.00, "currency": "EUR" } }]
function countAmountInOneWeek(data) {
var recordRemoveIndex;
var sum = data[0].operation.amount;
for (var i = 1; i < data.length; i++) {
var date1 = new Date(data[0].date);
var date2 = new Date(data[i].date);
const diffTime = Math.abs(date2.getTime() - date1.getTime());
const difference = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
//console.log(diffTime + " Time " + difference);
if (difference < 7) {
sum = sum + data[i].operation.amount;
recordRemoveIndex = i;
}
}
for (var i = 0; i <= recordRemoveIndex; i++) {
data.shift();
}
return [sum, data];
}
console.log(countAmountInOneWeek(data))