Search code examples
javascriptarraysfunctionobjectreturn

How to get object from a function


I am trying to get changed data object passed in function countAmountInOneWeek but when i try to get it i get message undefined.

This is for counting sum in past 7 days, after counting the sum i am deleting first seven days so i could count 7 days amount once more.

[{ "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);
  });
}

I need to get changed data object and sum

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))


Solution

  • In your readJSON function, change

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

    to

    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' } } ]
    

    You can then use or modify json2 as needed.

    The below code shows that it will return the proper values

    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];
    }