Looping through nested arrays

I have an array of arrays, actually a series of table rows.

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%"

If I come across certain content, i.e. "?" I want to assign this string to a new JSON object. I iterate over arrays with forEach loops as follows:

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

I need to be more explicit in my purpose. I have an array of row values ​​(in the form of arrays), some of which contain a table asking question, some of which are header lines, and some of them are table rows. I want to spit out a JSON object formatted something like this:

 {
"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%",
},

A JSON object may be more nested; this is apparently the easiest to handle.

+4
source share
1 answer

, , :

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

, .

+2

Source: https://habr.com/ru/post/1616098/


All Articles