Как получить объект из функции - PullRequest
0 голосов
/ 12 мая 2019

Я пытаюсь получить измененный объект данных, переданный в функцию 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))

1 Ответ

1 голос
/ 12 мая 2019

В вашей функции readJSON измените

   console.log("SUM " + sum[0]);
   console.log("SUM " + sum[1].data);

до

console.log(sum[0]); //1032700 
console.log(sum[1][0]); // because sum[1] is [ { date: '2016-02-15',user_id: 1,user_type: 'natural',type: 'cash_out',operation: {amount: 300, currency: 'EUR' } } ]

Затем вы можете использовать или изменять json2 по мере необходимости.

Код ниже показывает, что он вернет правильные значения

let json = [{ "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" } }] ;

//this code should be found within readJSON()
//we just run it straight away using json data above
//********************

var sum =	countAmountInOneWeek(json); 
console.log(sum[0]); //1032700 
console.log(sum[1][0]); // because sum[1] is [ { date: '2016-02-15',user_id: 1,user_type: 'natural',type: 'cash_out',operation: {amount: 300, 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();
    }
    //console.log(data);
    return [sum, data];
}
...