Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在嵌套数组中循环_Javascript_Arrays - Fatal编程技术网

Javascript 在嵌套数组中循环

Javascript 在嵌套数组中循环,javascript,arrays,Javascript,Arrays,我有一个数组,实际上是一系列表行 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"

我有一个数组,实际上是一系列表行

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
    }
  }
 {
"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对象,格式如下:

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
    }
  }
 {
"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对象可能嵌套得更为嵌套,这似乎是最容易处理的构建。

您可以使用可以返回对象的map函数,以便从元素数组中获取一系列问题:

function parseRow(element, index, array) {
   var questions = element.map(function(el,i){
       if(el.indexOf("?") > -1){
          return el;
       }
   });
}

这将为您提供一个包含问题的元素数组。

如果您想保持
forEach
循环,您需要为这两个循环使用一个公共闭包。或者,使用
map
,然后您确实可以将经过处理的内容返回到
parseRow
可以使用的表单中。当遇到
字符时,您具体想做什么?当遇到“?”时,我想将元素分配给一个newObj{“question”:element},但我还想使它成为一个新表的标记@JonathanBrooksthanks,我接受这个答案,因为它回答了基本问题,谢谢