2017-07-28 12 views
0

入力された配列(inputedJurisdictionArray)の要素と一致するJSON値(構造体jsonWithListOfStatesAndCounters)を抽出しようとしています。入力した配列に、単数または複数の州名(例:var inputedJurisdictionArray = ["Iowa", "California, Indiana, Delaware", "Florida"])が含まれています。この配列内の特異状態値は、最後に正常に処理されますが、複数の状態値は扱いにくい場所です。私はsplit()を使って別の配列に変換し、1つずつ処理できるようにしています。この入力された配列の状態の1つがjsonWithListOfStatesAndCountersの "状態"の値と一致するたびに、別のJSON構造に展開して、ブロックの最後に初期変数myJurisdictionJSONにプッシュします。私が抱えている問題は、これらのforEachループが完了すると、抽出されるべきvalとcounterではなく、元の値がmyJurisdictionJSONに残っているということです。 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);

関連する問題