如何从函数中获取对象

时间:2019-05-12 12:55:32

标签: javascript arrays function object return

我正在尝试获取通过countAmountInOneWeek函数传递的更改数据对象 但是当我尝试获取它时,我得到的消息未定义。

这是过去7天的总和,在计算完总和之后,我要删除前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 个答案:

答案 0 :(得分:1)

在您的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];
}