Problem of reading and copying json elements

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.

if (item.json.property_wc_1 | item.json.property_wc_2 != null){
        delete item.json.property_description;
        delete item.json.property_remplie;
        delete item.json.id;
        var prixtotal = ((item.json.property_wc_2 || 0 + item.json.property_wc_1 || 0) * item.json.property_prix_unit_ttc).toFixed(2);
        var remise = ((item.json.property_wc_2  || 0 + item.json.property_wc_1 || 0) * item.json.property_prix_remis_gr_ce_better_host).toFixed(2);
        item.json.property_prix_ttc_total = prixtotal; 
        item.json.property_prix_better_host_total = remise;
        test.WC.push(item.json);
    }


if (item.json.property_salon != null){
        delete item.json.property_description;
        delete item.json.property_remplie;
        delete item.json.id;
        var prixtotal = (item.json.property_salon * item.json.property_prix_unit_ttc).toFixed(2);
        var remise = (item.json.property_salon * item.json.property_prix_remis_gr_ce_better_host).toFixed(2);
        item.json.property_prix_ttc_total = prixtotal;
        item.json.property_prix_better_host_total = remise;
        test.Salon.push(item.json);
    }


if (item.json.property_chb_1 | item.json.property_chb_2 | item.json.property_chb_3 | item.json.property_chb_4 != null){
        delete item.json.property_description;
        delete item.json.property_remplie;
        delete item.json.id;
        item.json.property_prix_ttc_total = ((item.json.property_chb_1 || 0 + item.json.property_chb_2 || 0 + item.json.property_chb_3 || 0 + item.json.property_chb_4 || 0) * item.json.property_prix_unit_ttc).toFixed(2); 
        item.json.property_prix_better_host_total = ((item.json.property_chb_1 || 0 + item.json.property_chb_2 || 0 + item.json.property_chb_3 || 0 + item.json.property_chb_4 || 0) * item.json.property_prix_remis_gr_ce_better_host).toFixed(2);
        test.Chambre.push(item.json);
    }

I am a bit lost on this one, When you say average what tdo you mean? Can you share the json data so we can have a play?

here it is, function 1 includes the json data, function 2 the different operations.

Perfect, and what are you expecting the data to be?

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.

otherwise it is possible to prioritize the if (without using the and if, else …). Because my operation is not done correctly

Hi @simon_Piquemal,

Looking at your code I suspect the issue will be in that function, I just need to set aside some time to have a proper look.

1 Like

Okay then I may have found a workaround


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

Browser Console

1 Like

image

image

it does not recognize the syntax

var test = {
  "WC": [],
  "Salle_de_bain": [],
  "Cuisine": [],
  "Salon": [],
  "Chambre": [],
  "Terrasse": [],
  "Couloir": [],
  "Alinéa":[],
  "La_redoute": [],
  "Sklum": [],
  "Electro_dépôt": []}



items.forEach(item => {
    
    for (const key in item.json) {
        if (item.json[key] === null) {
            delete item.json[key];
                }
            }

    if (item.json.property_marque == "Sklum"){
        test.Sklum.push(item.json);
        };

    if (item.json.property_marque == "La Redoute"){
        test.La_redoute.push(item.json);
        };


    if (item.json.property_marque == "Alinéa"){
        test.Alinéa.push(item.json);
        };

    if (item.json.property_marque == "Electro dépôt"){
        test.Electro_dépôt.push(item.json);
        };



    if (item.json.property_wc_1 | item.json.property_wc_2 != null){
        delete item.json.property_description;
        delete item.json.property_remplie;
        delete item.json.id;
        var prixtotal = ((item.json.property_wc_2 || 0 + item.json.property_wc_1 || 0) * item.json.property_prix_unit_ttc).toFixed(2);
        var remise = ((item.json.property_wc_2  || 0 + item.json.property_wc_1 || 0) * item.json.property_prix_remis_gr_ce_better_host).toFixed(2);
        item.json.property_prix_ttc_total = Number(prixtotal); 
        item.json.property_prix_better_host_total = Number(remise);
        test.WC.push(item.json);
    };
    if (item.json.property_sdb_1 | item.json.property_sdb_2 != null){
        delete item.json.property_description;
        delete item.json.property_remplie;
        delete item.json.id;
        test.Salle_de_bain.push(item.json);
    };
    if (item.json.property_cuisine != null){
        delete item.json.property_description;
        delete item.json.property_remplie;
        delete item.json.id;
        item.json.property_prix_ttc_total = ((item.json.property_cuisine) * item.json.property_prix_unit_ttc).toFixed(2); 
        item.json.property_prix_better_host_total = ((item.json.property_cuisine) * item.json.property_prix_remis_gr_ce_better_host).toFixed(2);
        test.Cuisine.push(item.json);
    };
    if (item.json.property_chb_1 | item.json.property_chb_2 | item.json.property_chb_3 | item.json.property_chb_4 != null){
        delete item.json.property_description;
        delete item.json.property_remplie;
        delete item.json.id;
        item.json.property_prix_ttc_total = Number(((item.json.property_chb_1 || 0 + item.json.property_chb_2 || 0 + item.json.property_chb_3 || 0 + item.json.property_chb_4 || 0) * item.json.property_prix_unit_ttc).toFixed(2)); 
        item.json.property_prix_better_host_total = Number(((item.json.property_chb_1 || 0 + item.json.property_chb_2 || 0 + item.json.property_chb_3 || 0 + item.json.property_chb_4 || 0) * item.json.property_prix_remis_gr_ce_better_host).toFixed(2));
        test.Chambre.push(item.json);
    }; 
    if (item.json.property_salon != null){
        delete item.json.property_description;
        delete item.json.property_remplie;
        delete item.json.id;
        var prixtotal = item.json.property_salon * item.json.property_prix_unit_ttc.toFixed(2);
        var remise = item.json.property_salon * item.json.property_prix_remis_gr_ce_better_host.toFixed(2);
        item.json.property_prix_ttc_total = Number(prixtotal);
        item.json.property_prix_better_host_total = Number(remise);
        test.Salon.push(item.json);
    };       
    if (item.json.property_terrasse != null){
        delete item.json.property_description;
        delete item.json.property_remplie;
        delete item.json.id;
        test.Terrasse.push(item.json);
    };
    if (item.json.property_couloir_1 | item.json.property_couloir_2 != null){
        delete item.json.property_description;
        delete item.json.property_remplie;
        delete item.json.id;
        test.Couloir.push(item.json);
    };
});


var testv2 = test;


var listObj = [test.Chambre, test.Couloir, test.Cuisine, test.Salle_de_bain, test.Salon, test.Terrasse, test.WC ];


while (x = 0, x<test.Salon.length, x++ ){


};

for () {};

var yolo = test.Chambre[1];

return listObj[4];

but with the while loop it works so it’s not really a problem

This is failing because it is not a valid for loop so the error would be expected.

But if you are happy with the while loop then all is good .

1 Like

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 ?

Translated with DeepL Translate: The world's most accurate translator (free version)

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.