2016-12-17 77 views
1

多次嘗試終於我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" 
      }, ] 
     }] 
    }] 

}]    
+0

什麼API?第三方API? – zer00ne

+0

如果您對代碼進行格式化,則不需要這些註釋,至此支架即將關閉,並且即使對您來說也更易於閱讀/理解。然後,'mcoptions'和'greyOut',你確定要改變這些Arrays *(Array.push)*嗎?因爲這也會改變您添加到以前問題的所有引用。最後,我不明白你的問題,因爲我不知道你的應用程序是什麼樣的,它打算做什麼,哪些部分行爲不當,......基本上我不在你的腦海。我需要更多的信息,關於你的代碼應該做什麼,它做了什麼,不做什麼以及錯誤。 – Thomas

+0

@ zer00ne - 是的,這是一個第三方API(URL),我將把字符串化併發布JSON數據並依次獲取值 –

回答

0

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

以清除以前的數據並重新開始。

+0

感謝您的建議。實際上問題[]在其下面有多個值。所以我不能使用questions.length == 0.而是像問題[0]等。請你糾正我可以在哪裏執行索引問題[q1] .push .. –

+0

我發現你的問題很混亂,但我盡我所能去了解它。也許它應該是'if(questions [q1] .length == 0){questions [q1] .push(...); }' – Barmar