多次嘗試終於我here..Trust我會得到一個可能的解決方案後..迭代在環中特定的代碼只有一次
這裏是我的代碼:
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獲取請求,比較複選框的值並在另一個JSON格式的API中發佈。
問題1)比較之後,如果(項[I]。價值== 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循環中迭代其他值並在問題[]下顯示。現在,整個結構基於checkedValues進行迭代,而不是這種情況。
問題2)在greyOut:greyOut在mcoptions.push後,我想從API中獲取並添加另一個具有動態鍵和值的項目。像mcoptions [value:value,greyOut:gray [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? – zer00ne
如果您對代碼進行格式化,則不需要這些註釋,至此支架即將關閉,並且即使對您來說也更易於閱讀/理解。然後,'mcoptions'和'greyOut',你確定要改變這些Arrays *(Array.push)*嗎?因爲這也會改變您添加到以前問題的所有引用。最後,我不明白你的問題,因爲我不知道你的應用程序是什麼樣的,它打算做什麼,哪些部分行爲不當,......基本上我不在你的腦海。我需要更多的信息,關於你的代碼應該做什麼,它做了什麼,不做什麼以及錯誤。 – Thomas
@ zer00ne - 是的,這是一個第三方API(URL),我將把字符串化併發布JSON數據並依次獲取值 –