I have a problem when calculating the sum total of the products. In fact an object can be in several categories depending on the elements filled. So I created if and depending on the conditions the objects will go into an array. If an object has elements filled in room, bathroom, and corridor then the object and send in these 3 array via the push function. and it works well. But unfortunately the average operation for each category does not work well. So I wonder how to solve the problem.
When the json objects are in 2 or more categories the item.json.property_prix_ttc_total and item.json.property_prix_better_host_total give the right result.
this is a test for now so sorry for the name of these variables. To make it simple the if’s will put the objects in the right rooms. Then I go in each room to calculate the total sum. But I don’t understand why the for loop doesn’t work while the while loop works well. Maybe a bug of n8n.
Why do you think the for loop isn’t working? As a quick test I have added some console logging to it so that the output appears in the browsers console and it is showing that it is running and working.
for (const key in item.json) {
console.log(`Key: ${item.json[key]}`);
if (item.json[key] === null) {
console.log(`Deleting ${item.json[key]}`);
delete item.json[key];
}
}
in fact I still have the same problem. To summarize, 1 of the objects json was both in the room and in the living room. There is 1 item in the living room, so the calculation is simple: price including tax * 1. In the room there are 2 items so the calculation: price including tax * 2. But here is the calculation of the room is not done. However the 2 json objects are in 2 completely different tables. They are not next to each other. So where does the problem come from ? a bug of n8n ? Or a forgetfulness somewhere on my part ?
var listObj = [test.Chambre, test.Couloir, test.Cuisine, test.Salle_de_bain, test.Salon, test.Terrasse, test.WC ];
for (i = 0; i < listObj.length; i++) {
if (listObj[i] == test.Salon){
for (x = 0; x < listObj[i].length; x++ ){
var prixtotal = (listObj[i][x].property_salon * listObj[i][x].property_prix_unit_ttc).toFixed(2);
var remise = (listObj[i][x].property_salon * listObj[i][x].property_prix_remis_gr_ce_better_host).toFixed(2);
listObj[i][x].property_prix_ttc_total = Number(prixtotal);
};
}
else if (listObj[i] == test.Chambre){
for (x = 0; x < listObj[i].length; x++ ){
var op = [Number(listObj[i][x].property_chb_1 || 0 + listObj[i][x].property_chb_2 || 0 + listObj[i][x].property_chb_3 || 0 + listObj[i][x].property_chb_4 || 0)];
var prixtotal = (op[0] * listObj[i][x].property_prix_unit_ttc).toFixed(2);
var remise = (op[0] * listObj[i][x].property_prix_remis_gr_ce_better_host).toFixed(2);
listObj[i][x].property_prix_ttc_total = Number(789);
};
};
};
A yes to specify if I reverse the data in the if and else then the reverse occurs. The price of the room is well calculated not that of the living room.