4
我有一個數組數組,實際上是一系列的錶行。通過嵌套數組循環
0: Array[5]
0: ""
1: ""
2: "Is there at least one teacher or other adult in this school that you can talk to if you have a problem?"
3: ""
4: ""
1: Array[5]
0: ""
1: ""
2: "Yes"
3: "No"
4: "Not sure"
2: Array[5]
0: "Grade"
1: "6th"
2: "55%"
3: "20%"
4: "25%"
如果我遇到某些內容,即「?」我想將該行分配給新的JSON對象。我正在循環訪問forEach循環,如下所示:
function parseRow(element, index, array) {
element.forEach(parseCell);
}
function parseCell(element, index, array) {
if (element.indexOf("?") > -1) { // if it is a question
//do something to tell indicate this is a question row
} else {
// table cell
}
}
我需要更加明確地實現我的目標。我有一個行值(作爲數組),其中一些包含一個表定義的問題,其中一些是標題行,其中一些是錶行。我要吐了出來格式是這樣的一個JSON對象:
{
"question": "Is there at least...",
"Category": "Overall",
"Division" : "All",
"Yes" : "65.00%",
"No": "11.70%",
"Not Sure" : "23.30%",
},
{
"question": "Is there at least...",
"Category" : "Grade",
"Division" : "6th",
"Yes" : "65.00%",
"No": "11.70%",
"Not Sure" : "23.30%",
},
{
"question": "Is there at least...",
"Category" : "Grade",
"Division" : "7th",
"Yes" : "65.00%",
"No": "11.70%",
"Not Sure" : "23.30%",
},
的JSON對象可以是多個嵌套,這只是似乎是最容易處理的構建。
如果你想保留'forEach'循環,你需要一個通用閉包。或者,使用'map',然後確實可以將處理後的內容返回到'parseRow'可以使用的表單中。 –
我與@BartekBanachewicz。在遇到「?」字符時,你想要做什麼? –
當遇到「?」我想將元素賦值給newObj {「question」:element},但我也想將它作爲新表的標記。 @JonathanBrooks – icicleking