0

I am using p5 loadJSON to import data, the data gives an array with so many records and sub-arrays.

For example, data.records[i].form_values["94d5"] gives me a name.

I am currently using a for loop to go through data.records.length and give me an array of some key pieces of data relative to each record I want to see.

Problem: I want to loop through the records and get the value of: data.records[i].form_values["94d5"].choice_values["0"], but some records don't have form_values["94d5"], for example. I just want to skip empty sections (leave undefined like empty items) insted i get an error that choice_values is undefined.

My loop is:

function gotData(data){
    var i = 0;
    var statusFind = data.records[i].status;

    for(i = 0; i < data.records.length; i++) {
        var returned = [data.records[i].form_values["6f71"], data.records[i].form_values.b059, data.records[i].form_values["94d5"], data.records[i].form_values.b62d, data.records[i].form_values["3493"].choice_values["0"], data.records[i].status, data.records[i].form_values.af80];
        for (j = 0; j < returned.length; returned++){
          if (returned[j] == searchKey) {
            var result = [returned[0], returned[1], returned[2], returned[3], returned[4], returned[5], returned[6]];
            array.push(result);

            write();
          }
        }
    }
}
jHilscher
  • 1,810
  • 2
  • 25
  • 29
Jim
  • 570
  • 5
  • 15
  • 1
    if (!data.records[i].form_values["94d5"]) { continue; } - this code is to skip items which don't have form_values["94d5"] – zhuravlyov Oct 02 '17 at 11:46
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Teemu Oct 02 '17 at 11:55
  • @zhuravlyov that worked well but it skips all the data in the continued loop; instead of just that one field. – Jim Oct 02 '17 at 22:02

0 Answers0