【问题标题】:Iterate a particular code in for loop only once仅在 for 循环中迭代特定代码一次
【发布时间】:2016-12-17 09:04:27
【问题描述】:

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

这是我的代码:

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 == checkedValues[c]) 后,我想要

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

在 for 循环中只显示一次。 mCoptions。 push 和 gray 应该在 for 循环中迭代其他值并显示在 questions[] 下。现在整个结构基于checkedValues进行迭代,这不应该是这种情况。

问题 2) 在 mcoptions.push 中的 grayOut : grayOut 之后,我想从 API 中获取并添加另一个具有动态键和值的项目。像 mcoptions[value: value, grayOut: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"
            }, ]
        }]
    }]

}]            

【问题讨论】:

  • 什么 API?第三方 API?
  • 如果你格式化你的代码,你就不需要这些注释了,其中括号关闭了循环,并且更容易阅读/理解,即使对你来说也是如此。那么mcoptionsgreyOut,您确定要对这些数组(Array.push) 进行变异吗?因为这也会改变您添加到之前问题的所有引用。最后,我不明白你的问题,因为我不知道你的应用程序是什么样子,它打算做什么,哪些部分行为不端,...,基本上我不在你的脑海里。我需要更多信息,关于你的代码应该做什么,它做什么,它不做什么以及有什么问题。
  • @zer00ne - 是的,这是一个第三方 API(URL),我将在其中对 JSON 数据进行字符串化和发布,然后依次获取值
  • 您的 JSON 无效。 Javascript 允许您将 , 放在数组的最后一个元素之后,但 JSON 不允许。
  • @Thomas - 我明白你的意思。请找到我添加了 JSON 的编辑版本。我希望现在您能够识别代码。我从此 JSON 中获取值,比较用户在 UI 中检查的值,并将相同结构中的相应 JSON 值发布到另一个 URL。谢谢。

标签: javascript jquery json ajax for-loop


【解决方案1】:

1) 您可以在向questions 推送新项目之前测试其中是否已经有东西:

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

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

【讨论】:

  • 感谢您的建议。实际上 questions[ ] 在此之下拥有多个值。所以我不能使用 questions.length == 0。而是像 questions[0] 等。你能否更正我可以在哪里实现索引,如 questions[q1].push..
  • 我发现您的问题非常令人困惑,但我已尽力理解它。也许应该是if (questions[q1].length == 0) { questions[q1].push(...); }