javascript - 仅在 for 循环中迭代一次特定代码

标签 javascript jquery json ajax for-loop

经过几次尝试,我终于来了..相信我会得到一个可能的解决方案..

这是我的代码:

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 < options.length;i++)
        {
            for(var c=0; c < 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<grey.length;g++)
                    {
                        greyOut.push({
                            greyOut  : grey[g]
                        });
                    }
                }}
        }// for loop close
    }//fieldtype
}// q1 in questions

此代码将从 API 执行 GET 请求,比较复选框值并以 JSON 格式发布到另一个 API。

问题1)比较 if(options[i].value == checkValues[c]) 后,我想要

           questions.push({
                seqNum : questions1[q1].seqNum, 
                qtext : questions1[q1].qtext, 
                qimage : questions1[q1].qimage,
                fieldtype : questions1[q1].fieldtype,
                mcoptions : mcoptions
            });

在for循环中只显示一次。 mC 选项。 Push 和 Gray 应该在 for 循环中迭代其他值并显示在 questions[] 下。现在整个结构基于checkedValues进行迭代,但事实不应该是这样的。

问题 2) 在 mcoptions.push 中的 greyOut : greyOut 之后,我想从具有动态键和值的 API 中获取并添加另一项。像 mcoptions[value: value, greyOut:grey[g] , x: 1]

问题 3)如果我转到同一部分并更改答案并单击提交(POST 请求),我希望部分 [0] 被删除并完全替换。

请帮助并分享您的宝贵建议。非常感谢您。

JSON 格式:

[{
    "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"
            }, ]
        }]
    }]

}]            

最佳答案

1) 您可以在向其中推送新项目之前测试问题中是否已包含某些内容:

if (questions.length == 0) {
    questions.push({
        seqNum : questions1[q1].seqNum, 
        qtext : questions1[q1].qtext, 
        qimage : questions1[q1].qimage,
        fieldtype : questions1[q1].fieldtype,
        mcoptions : mcoptions
    });
}

2) 执行 API 请求,并在回调中将附加属性添加到 mcoptions 值。您可以使用 IIFE 来创建一个闭包来保存对回调中需要更新的对象的引用。

var newmcoption = {
    value : options[i].value,
    greyOut : greyOut,
};
mcoptions.push(newmcoption);
(function(mcoption) {
    CallAPI(function(response) {
        mcoption.x = response;
    });
})(newmcoption);

3)我不太确定我理解问题的这一部分,但我认为你可以这样做:

sections.length = 0;
sections.push({
    questions:questions
});

清除以前的数据并重新开始。

关于javascript - 仅在 for 循环中迭代一次特定代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41196997/

相关文章:

arrays - 获取jq中的对象数组索引

javascript - 为每个选项值创建单独的下拉菜单

javascript - jQuery 验证 : Validate dynamicly generated fields only on the first field

javascript - 是否可以使用 java 脚本选择具有相同类名的多个 html 元素?

asp.net-mvc - 如何使用 ajax get 或 post 在 mvc 中使用参数将数据从 View 传递到 Controller

php - 从数据库格式化文本

javascript - 如何在解析时更改 JSON 属性名称

javascript - 获取数据并将标题添加到 Json 对象

php - 在循环数据中选择选项时显示 div

arrays - 将 JSON 对象列表解码为 perl 中的哈希数组