我有一个数组,实际上是一系列表格行。
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
可以使用。 - Bartek Banachewicz?
字符时,你具体想要做什么? - jonny