08.04.2024, 13:16
I need help.
Not so good with Javascript...
I have this code:
My problem I can't iterate on the array on the function:
for example with
//last day
let day=getdata(classImage,-1,0,1);
this don't work. Lentgth return 0...
for (let i = 0; i < day .length; i++) {
console.log(day[i])
}
however a console.log of day show all result... and a length. I gess there is something with scope/timelaps but I try also in jquery, with map... all same.
If our so nice admin could help me a bit, greatly appreciated.
Not so good with Javascript...
I have this code:
Code:
function getdate(offset) {
var date = new Date();
date.setDate(date.getDate() + offset+1);
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
return { year, month, day };
}
function getdata(nom, dayBefore, dayBase,total) {
var result = [];
var trendsName = [];
var trendsAddress = [];
var myJsonString='';
//FIND GA NAME AND ADDRESS
$.each(grp.tag(nom), function(id, object) {
trendsName.push(object.name);
trendsAddress.push(Scada.encodeGroupAddress(object.address));
});
// CREATE ARGUMENT
var data = JSON.stringify({
trends: trendsAddress,
dates: {
start: getdate(dayBefore),
end: getdate(dayBase),
},
// resolution: 86400,
// timestamps: true,
});
// TRENDS.LP POST
$.post('/user/trends.lp', data, function(resp) {
$.each(resp, function(id, object) {
// SUM TO TOTAL
if (total>0) {
let totVal = 0;
for (let l = 0; l < object.length; l++) {
totVal += object[l];
}
result.push(trendsName[id],totVal);
// NO TOTAL
} else {
result.push(trendsName[id],object);
}
});
});
return result;
}
My problem I can't iterate on the array on the function:
for example with
//last day
let day=getdata(classImage,-1,0,1);
this don't work. Lentgth return 0...
for (let i = 0; i < day .length; i++) {
console.log(day[i])
}
however a console.log of day show all result... and a length. I gess there is something with scope/timelaps but I try also in jquery, with map... all same.
Code:
Array []
0: "PV - Active Energy No reset"
1: 16854
2: "General- Active Energy No reset"
3: 19329
length: 4
<prototype>: Array []
If our so nice admin could help me a bit, greatly appreciated.
-----------
FRANCE SMARTHOME & SMARTBUILDING INTEGRATION
SE ECO EXPERT
FRANCE SMARTHOME & SMARTBUILDING INTEGRATION
SE ECO EXPERT