Iterate a particular code in for loop only once
<p>After several tries finally I am here..Trust I will get a possible solution..</p>

<p>Here is my code:</p>

<pre><code>var sections=[];

sections.push({
    questions:questions
});

for(var q1  in questions1){
    if (questions1[q1].fieldtype == "checkbox") {
        var options=questions1[q1].mcoptions;
        for(var i=0;i &lt; options.length;i++)
        {
            for(var c=0; c &lt; checkedValues.length; c++)
            {
                if(options[i].value == checkedValues[c])
                {
                    questions.push({
                        seqNum : questions1[q1].seqNum, 
                        qtext : questions1[q1].qtext, 
                        qimage : questions1[q1].qimage,
                        fieldtype : questions1[q1].fieldtype,
                        mcoptions : mcoptions
                    });

                    mcoptions.push({
                        value : options[i].value,
                        greyOut : greyOut,
                    }); 

                    var grey=options[i].greyOut;
                    for(var g=0;g&lt;grey.length;g++)
                    {
                        greyOut.push({
                            greyOut  : grey[g]
                        });
                    }
                }}
        }// for loop close
    }//fieldtype
}// q1 in questions
</code></pre>

<p>This code will perform GET request from API,compare checkbox value and post in another API in JSON format.</p>

<p>Question 1) After comparing if(options[i].value == checkedValues[c]) , I want        </p>

<pre><code>           questions.push({
                seqNum : questions1[q1].seqNum, 
                qtext : questions1[q1].qtext, 
                qimage : questions1[q1].qimage,
                fieldtype : questions1[q1].fieldtype,
                mcoptions : mcoptions
            });
</code></pre>

<p>to display only once which is in for loop. mCoptions. push and grey should iterate other values in for loop and display under questions[]. Now the whole structure iterates based of checkedValues which shouldn't be the case.</p>

<p>Question 2) After greyOut : greyOut in mcoptions.push, I want to fetch and add another one item from API which has dynamic key and value. Like mcoptions[value: value, greyOut:grey[g] , x: 1]</p>

<p>Question 3) If I go to the same section and change my answer and click submit(POST request), I want sections[0] to be removed and replaced entirely.</p>

<p>Please help and share your valuable suggestions.. Thank you so much in advance. </p>

<p>JSON format:</p>

<pre><code>[{
    "sessionName": "XYZ",
    "ID": "123",
    "sections": [{
        "slabel": "Development",
        "qlabel": "text",
        "questions": [{
            "seqNum": "1",
            "qtext": "What level of understanding would you say there is of Dev principles and methods?",
            "qimage": "",
            "fieldtype": "checkbox",
            "mcoptions": [{
                "value": "none",
                "greyOut": [
                    "1.1",
                    "1.2"
                ],
                "dev": "0"
            }, {
                "value": "some",
                "greyOut": [],
                "cog": "10"
            }, ]
        }]
    }, {
        "slabel": "Development111",
        "qlabel": "text",
        "questions": [{
            "seqNum": "1",
            "qtext": "What level of understanding would you say there is of Dev principles and methods?",
            "qimage": "",
            "fieldtype": "checkbox",
            "mcoptions": [{
                "value": "none",
                "greyOut": [
                    "1.1",
                    "1.2"
                ],
                "dev": "0"
            }, {
                "value": "some",
                "greyOut": [],
                "cog": "10"
            }, ]
        }]
    }]

}]            
</code></pre>