title: JS - Pushing data into JSON structure using forEach loops on Arrays
body: <p>I am attempting to extract JSON values (from structure called <code>jsonWithListOfStatesAndCounters</code>) if it matches with an element in my inputted array (<code>inputedJurisdictionArray</code>). My inputed array contains sting values that include singular or multiple state names (i.e. <code>var inputedJurisdictionArray = ["Iowa", "California, Indiana, Delaware", "Florida"]</code>). The singular State values in this array are handled normally at the end, but the multiple state values is where it gets tricky. I am using <code>split()</code> in order to turn them into another array so they can get processed one by one. Anytime one of the states from this inputed array matches with a "state" value in <code>jsonWithListOfStatesAndCounters</code>, I am extracting it into another JSON structure and pushing it at the end of every block into my initial variable <code>myJurisdictionJSON</code>. The problem I am having is that once these forEach loops are completed, I am still left with my original values in <code>myJurisdictionJSON</code>, instead of the val and counter that should be extracted. The <code>jsonWithListOfStatesAndCounters</code> definitely contains the values that should match with the elements of my <code>inputedJurisdictionArray</code>, but the information is not being pushed into <code>myJurisdictionJSON</code>. What am I doing wrong? Any tips/pointers will be helpful.</p>
  
  <pre><code>var myJurisdictionJSON = [{
      jurisdiction_val: 'jurisdiction_val',
      jurisdiction_counter: 'jurisdiction_counter'
  }];
  
  inputedJurisdictionArray.forEach(function each(item) {
      if (Array.isArray(item)) {
          item.forEach(each);
      } else {
          var jurisdictionInput = item;
          jsonWithListOfStatesAndCounters.forEach(function each(item) {
              if (Array.isArray(item)) {
                  item.forEach(each);
              } else { 
                  if (jurisdictionInput.includes(",") === true){//Checking if more than one jurisdiction in string
                      var jurisdictionArr = jurisdictionInput.split(", ");
                      var jurisdictionCounter = item.jurisdictionCounter;
                      var jurisdictionState = item.jurisdictionState;
                      jurisdictionArr.forEach(function(element) {
                          if (myJurisdictionJSON.jurisdiction_counter == 'jurisdiction_counter'){ // If nothing is pushed into our predefined JSON object
                              if (jurisdictionState.toLowerCase() == trim(element.toLowerCase())) {
                                  var jurisdictionJSON_inner = {
                                      jurisdiction_val: element,
                                      jurisdiction_counter: jurisdictionCounter
                                  };
                                  myJurisdictionJSON.push(jurisdictionJSON_inner);
                                  return;
                              }
                          }else if (myJurisdictionJSON.jurisdiction_counter != 'jurisdiction_counter'){ // if an item has been pushed into myJurisdictionJSON, append the next items
                              var jurisdictionCounter = item.jurisdictionCounter;
                              var jurisdictionState = item.jurisdictionState;                                
                              if (jurisdictionState.toLowerCase() == trim(jurisdictionInput.toLowerCase())) {
                                  jurisdictionJSON_inner.jurisdiction_val = jurisdictionJSON_inner.jurisdiction_val + ", " + jurisdictionInput;
                                  jurisdictionJSON_inner.jurisdiction_counter = jurisdictionJSON_inner.jurisdiction_counter + ", " + jurisdictionCounter;
                                  myJurisdictionJSON.push(jurisdictionJSON_inner);
  
                                  return;
                              }
                          }
                      });
                  }
                  else{// if only one jurisdiction state in jurisdictionInput string
                      var jurisdictionCounter = item.jurisdictionCounter;
                      var jurisdictionState = item.jurisdictionState;               
                      if (jurisdictionState.toLowerCase() == trim(jurisdictionInput.toLowerCase())) {
                          var jurisdictionJSON_inner = {
                              jurisdiction_val: jurisdictionInput,
                              jurisdiction_counter: jurisdictionCounter
                          };
                          myJurisdictionJSON.push(jurisdictionJSON_inner);
  
                          return;
                      }
                  }   
              }       
          });
  </code></pre>