Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

cimulative key and value using forEach

I want to merge each key and value each time I treverse elements, but whenever I pass for loop, the overwrite the newest new instead of merge the new elments = old elements + new elements here is my forEach:

        let ff = {};
        let tmp_ff = {};

        this.choosen_products.forEach((element, index, array) => {
            //q_p1_fullname
            let tmp1 = "q_p"+(index+1)+"_fullname"; //each time, tmp1 key are different
            let tmp2 = "q_p"+(index+1)+"_code";
            let tmp3 = "q_p"+(index+1)+"_category";
            let tmp4 = "q_p"+(index+1)+"_cost";
            let tmp5 = "q_p"+(index+1)+"_margin";
            let tmp6 = "q_p"+(index+1)+"_sell";
            console.log(element.p_fullname + " " + element.p_category + "       "  +  tmp1);
            /*
            ff.this.tmp1 = element.p_fullname, //I try this, no help
            ff.this.tmp2 = element.p_code,
            ff.this.tmp3 = element.p_category,
            ff.this.tmp4 = element.p_cost,
            ff.this.tmp5 = element.p_margin,
            ff.this.tmp6 = element.p_sell,
            */
            tmp_ff = {
                [tmp1]: element.p_fullname, //works but only the most recent loop, how to merged??
                [tmp2]: element.p_code,
                [tmp3]: element.p_category,
                [tmp4]: element.p_cost,
                [tmp5]: element.p_margin,
                [tmp6]: element.p_sell,
            };
            let merged = { tmp_ff, ff }; 

            Object.keys(tmp_ff).forEach(key => {
                console.log("[previewBtn-choosen_products2]" + key, tmp_ff[key]); //not cimulative
            });


            console.log("[previewBtn-choosen_products2]        " ); // 100, 200, 300
            console.log(index); // 0, 1, 2
            console.log(array); // same myArray object 3 times
            bodyData.push(element);

            console.log("[previewBtn-choosen_products2]" + Object.keys(merged).tmp1);
        });`

I want these 6 keys and elements sum-up, which means the second forEach loop, total the Object has 12 and 18 key and value etc. Now only the newest one exist, I try to do

merged = { tmp_ff, ff }; 

but no specific help yet, I see merged has only length of 2…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My final result want to be like this:

tmp_ff = {
                    q_p1_fullname: element.p_fullname,
                    q_p1_code: element.p_code,
                    q_p1_category: element.p_category,
                    q_p1_cost: element.p_cost,
                    q_p1_margin: element.p_margin,
                    q_p1_sell: element.p_sell,
                    q_p2_fullname: element.p_fullname,
                    q_p2_code: element.p_code,
                    q_p2_category: element.p_category,
                    q_p2_cost: element.p_cost,
                    q_p2_margin: element.p_margin,
                    q_p2_sell: element.p_sell,

                };

>Solution :

without data I can’t create an example, but this should build your dictionary in one go:

const tmp_ff = Object.fromEntries(
  this.choosen_products.flatMap((element, index) => [
    [`q_p${index + 1}_fullname`, element.p_fullname],
    [`q_p${index + 1}_code`, element.p_code],
    [`q_p${index + 1}_category`, element.p_category],
    [`q_p${index + 1}_cost`, element.p_cost],
    [`q_p${index + 1}_margin`, element.p_margin],
    [`q_p${index + 1}_sell`, element.p_sell],
  ])
);

console.log(tmp_ff);

I just don’t find this data structure useful. It’s definitely harder to work with than the array.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading