2017-09-27 4 views
0

1つのオブジェクトを逆アセンブルして特別な方法で新しいオブジェクトに書き込む必要がある2つのforループがあります。しかし、何らかの理由で私のオブジェクトデータが上書きされ、最終結果が最後のエントリにすぎません。ここに私のコードは次のとおりです。ループで作業するときにJavascriptオブジェクトが上書きされる

let j = 0, k = 0, tempCities = [];  
for (let stateKey in this.countries[countryKey].states) { 
     if (this.countries[countryKey].states.hasOwnProperty(stateKey)) { 
     this.states[j] = this.states[stateKey].name; 

     for (let cityKey in this.states[stateKey].cities) { 
      if (this.states[stateKey].cities.hasOwnProperty(cityKey)) { 
      tempCities[k] = this.cities[cityKey].name; 
      } 
      k++; 
     } 

     k = 0; 
     this.cities[this.states[j]] = tempCities; 
     } 

     j++; 
    } 

問題はこの1つは、外側のループのこれまでの繰り返しthis.cities[this.states[j]] = tempCities

に上書きされます私はthis.states[j]するオブジェクトのキーを持っている必要があり、私はそれらのすべてを持っている必要があるということです上書きはありません。

これを修正する方法についてのアドバイスは非常に高く評価されます!次のようにあなたは、あなたのデータをフラット化する必要が

また、ここで私は(Firebaseからの発信)で動作するようにしようとしているサンプルデータがある

{ 
    "cities": { 
     "-KuzIBwGCbJhAswAcOTk": { 
      "cityCounter": 1, 
      "name": "Sceaux-du-Gâtinais", 
      "state": "-KuzIBslq88OXpE_L0iy" 
     }, 
     "-Kv1e75Dd2pjnropd63W": { 
      "cityCounter": 1, 
      "name": "Labelle", 
      "state": "-Kv1e724mJTS-6E_DZh1" 
     }, 
    }, 
    "countries": { 
     "-KuzIBoxiP2oksxhxFX6": { 
      "countryCounter": 1, 
      "name": "France", 
      "states": { 
       "-KuzIBslq88OXpE_L0iy": true 
      } 
     }, 
     "-Kv1e6wxA9RwYyEYO27e": { 
      "countryCounter": 2, 
      "name": "United States", 
      "states": { 
       "-Kv1e724mJTS-6E_DZh1": true 
      } 
     } 
    }, 
    "states": { 
     "-KuzIBslq88OXpE_L0iy": { 
      "cities": { 
       "-KuzIBwGCbJhAswAcOTk": true 
      }, 
      "country": "-KuzIBoxiP2oksxhxFX6", 
      "name": "Centre Region", 
      "stateCounter": 1 
     }, 
     "-Kv1e724mJTS-6E_DZh1": { 
      "cities": { 
       "-Kv1e75Dd2pjnropd63W": true, 
       "-Kv1eEb3D4pqd7l14KCg": true 
      }, 
      "country": "-Kv1e6wxA9RwYyEYO27e", 
      "name": "FL", 
      "stateCounter": 2 
     } 
    } 
} 
+0

愚かな質問: 'j = 0'のどこかで宣言しますか? – Bartheleway

+2

@dimitriサンプルデータを使ってコードを修正できるので、forループの入力を知ることができます –

+1

都市とのループの前に 'tempCities = []'を宣言し、 'tempCities.push(this.cities [cityKey] .name) 'を返します。これは 'this.states'変数にも有効です。 – Bartheleway

答えて

0

{ 
    "coutries": { 
     "-JiGh_31GA20JabpZBfa": { 
      "name": "United States", 
      "states": { 
       "-JiGh_31GA20JabpZBfb": true 
      } 
     } 
    }, 
    "states": { 
     "-JiGh_31GA20JabpZBfb": { 
      "name": "Florida", 
      "country": "-JiGh_31GA20JabpZBfa", 
      "cities": { 
       "-JiGh_31GA20JabpZBfc": true 
      } 
     } 
    }, 
    "cities": { 
     "-JiGh_31GA20JabpZBfc": { 
      "name": "Miami", 
      "state": "-JiGh_31GA20JabpZBfb", 
      "visited_by": { 
       "P1yjH4GgcFQcJUHULmTgMLC68w64": true 
      } 
     } 
    }, 
    "users": { 
     "P1yjH4GgcFQcJUHULmTgMLC68w64": { 
      "cities_visited": { 
       "-JiGh_31GA20JabpZBfc": true 
      } 
     } 
    } 
} 
関連する問題