2017-07-28 55 views
0

如果它与我输入数组中的元素(inputedJurisdictionArray)匹配,我试图提取JSON值(来自名为jsonWithListOfStatesAndCounters的结构)。我的输入数组包含了包含单数或多个状态名称的sting值(即var inputedJurisdictionArray = ["Iowa", "California, Indiana, Delaware", "Florida"])。这个数组中的奇异状态值通常在最后处理,但是多个状态值是棘手的。我使用split()将它们转换为另一个数组,以便它们可以逐个处理。无论何时,这个输入数组中的某个状态与jsonWithListOfStatesAndCounters中的“状态”值相匹配,我将它解压缩到另一个JSON结构中,并将其在每个块的末尾推入我的初始变量myJurisdictionJSON。我遇到的问题是,一旦这些forEach循环完成,我仍然保留我的原始值myJurisdictionJSON,而不是应提取的val和counter。 jsonWithListOfStatesAndCounters确实包含应与我的inputedJurisdictionArray的元素匹配的值,但信息未被推入myJurisdictionJSON。我究竟做错了什么?任何提示/指针都会有所帮助。JS - 在数组中使用forEach循环将数据推送到JSON结构中

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

回答

0

我不完全确定输出是你想要的,但它很接近。

// input data as per your example 
 
let inputedJurisdictionArray = [ 
 
    'Iowa', 
 
    'California, Indiana, Delaware', 
 
    'Florida' 
 
]; 
 

 
// I had to make this part up. It's missing from the example 
 
let jsonWithListOfStatesAndCounters = [{ 
 
    jurisdictionCounter: 2, 
 
    jurisdictionState: 'Florida' 
 
    }, 
 
    { 
 
    jurisdictionCounter: 4, 
 
    jurisdictionState: 'Indiana' 
 
    }, 
 
    { 
 
    jurisdictionCounter: 3, 
 
    jurisdictionState: 'Texas' 
 
    } 
 
]; 
 

 
// first, fix up inputedJurisdictionArray 
 
// reduce() loops over each array element 
 
// in this case we're actually returning a LARGER 
 
// array instead of a reduced on but this method works 
 

 
// There's a few things going on here. We split, the current element 
 
// on the ','. Taht gives us an array. We call map() on it. 
 
// this also loops over each value of the array and returns an 
 
// array of the same length. So on each loop, trim() the whitespace 
 
// Then make the accumulator concatenate the current array. 
 
// Fat arrow (=>) functions return the results when it's one statement. 
 
inputedJurisdictionArray = inputedJurisdictionArray.reduce(
 
    (acc, curr) => acc.concat(curr.split(',').map(el => el.trim())), [] 
 
); 
 

 
// now we can filter() jsonWithListOfStatesAndCounters. Loop through 
 
// each element. If its jurisdictionState property happens to be in 
 
// the inputedJurisdictionArray array, then add it to the 
 
// myJurisdictionJSON array. 
 
let myJurisdictionJSON = jsonWithListOfStatesAndCounters.filter(el => 
 
    inputedJurisdictionArray['includes'](el.jurisdictionState) 
 
); 
 

 
console.log(myJurisdictionJSON);

相关问题