gpt4 book ai didi

javascript - 如何从函数中获取对象

转载 作者:行者123 更新时间:2023-12-02 23:40:32 24 4
gpt4 key购买 nike

我正在尝试获取在函数 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))

最佳答案

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

关于javascript - 如何从函数中获取对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56099453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com