Home » Jquery » jquery – Facing issue while trying to iterate through the dictionary in JavaScript which is used to download the form data in a text/yaml file

jquery – Facing issue while trying to iterate through the dictionary in JavaScript which is used to download the form data in a text/yaml file

Posted by: admin February 22, 2020 Leave a comment

Questions:

I am trying to download the form data which the user enters through a utility which i have built on html and node js. I have defined a dictionary data where I am capturing the data and adding it to the payload in a particular format.

I have a field Number of Chasis based on which I need to populate the data UUT1 and UUT2(as shown in the for loop). The value of number of chasis is currently 2 although the loop is just iterating for the 0th component and I am unable to get anything for the 1st iteration.

Can anyone please help me in solving this issue. Attaching the sample code here and a fiddle for the entire utility.

const Ctids = document.querySelectorAll('.CONTROLLER_ID');
    const mcIps = document.querySelectorAll('.mc_ip');
    const mcNetmasks = document.querySelectorAll('.Netmask_IP');
    const mcGateways = document.querySelectorAll('.Gateway_IP');
    const mc = document.querySelectorAll('.RBOD_MC');
    const ec = document.querySelectorAll('.RBOD_EC');
    const fu = document.querySelectorAll('.RBOD_FU');
    const sc = document.querySelectorAll('.RBOD_SC');
    const step = oForm.chassis.value;

        let payload = '';
        //Creating the data elements to be cpatured in the downloaded yaml file 

        for (let i = 0; i < step; i++) {
        //  console.log('Walking east one step');

            payload+= '\n -  # UUT ' + i + ' configuration'+'\n    -  # Chassis ' + i + ' configuration'+ String.fromCharCode(10);

            const addToPayload = (object, whitespace) => {

            for (const key of Object.keys(object)) {
                payload += key + '\n';
                const details = object[key];
                for (const key1 of Object.keys(details)){
                const value = details[key1];
                str=value[0];
                com=value[2];
                val=value[1];
                whitespace=value[3];
                var input= JSON.stringify(val);

                if (Array.isArray(val) && val.length){
                    payload += whitespace + str + ':' +  com + '\n'+'                '+input;
                    }
                //else if (val==document.getElementById('hba_ports1').value || val==document.getElementById('hba_ports').value ){
                //  var out= val.split(',');
                    //payload += whitespace + str + ':' + com + JSON.stringify(out);
                    //}
                else if (val==document.getElementById('ip1').value || val==document.getElementById('ip2').value){
                    var out1= val.split(',');
                    payload += whitespace + str + ':' +  com + '\n'+'                    '+ JSON.stringify(out1);   
                    }
                else if (!val || !val.length) {
                payload += str
                }
                    else{payload += whitespace + str + ':' + val + com ;}
                }   
            }
            }

        //if((document.getElementById("ip1").disabled || document.getElementById("ip2").disabled) && oForm.ctrls.value==1){
    //  if(i==0){
        const data = {
            "   PDU:": {
            "PDU_IP":['     PDU_IP',' '+ oForm.ip.value,"   #PDU IP",'  '+'\n'],
            "PDU_LEFT":['       PDU_LEFT',[oForm.lo.value]," # left_outlet(s) ",'   '+'\n'],
            "PDU_RIGHT":['      PDU_RIGHT',[oForm.ro.value]," # right_outlet(s) ", '    '+'\n']
            },  
            "   \n  CONTROLLER: # Controller settings ": {          
                "Controller_ID#1": ["       -  # Controller A for RBOD/EBOD"],
                "Netmask IP_A":["           MC_IP",' '+mcIps[i].value,'  # RBOD MC IP',''+'\n'],
                "MC_Netmask_A": ["          MC_Netmask",' '+mcNetmasks[i].value,'  # RBOD MC Netmask',''+'\n'],
                'MC_Gateway_A':['           MC_Gateway',' '+mcGateways[i].value,'  # RBOD MC Gateway',''+'\n'],
                'MC_A': ['          MC',' '+mc[i].value,'  # RBOD MC port',''+'\n'],
                'SC_A':['           SC',' '+sc[i].value,'  # RBOD SC port',''+'\n'],
                'FU_A':['           FU',' '+fu[i].value,'  # RBOD FU port',''+'\n'],
                'EC_A':["           EC",' '+ec[i].value,'  # EC port on RBOD or GEM port on JBOD',''+'\n'],
                'Controller_ID#2': ["           ID",' '+Ctids[i].value,"    #Controller ID, A or B",''+'\n'],   
            }
        };
        addToPayload(data, ' ' );
        //  }

        }

Fiddle

How to&Answer: