2017-02-18 7 views
0

大きな配列が与えられているので、型が "顧客" "店"か "住所"かによって異なる配列にデータを転送する必要がありますが、次のようになりますエラーと私;わからない理由定義がわからないエラー

/* 
Exception: ReferenceError: store is not defined 
[email protected]/1:56:17 
@Scratchpad/1:105:1 
*/ 


var allData = [ 
{type:"store", data:{store_id: 297, name: "Scotiabank - Main Branch",  address_id: 1023}}, 
{type:"store", data:{store_id: 614, name: "Scotiabank - Hamilton", address_id: 1984}}, 
{type:"customer", data:{customer_id: 24, store_id:614, first_name: "Jonathan", last_name: "Pym", email: "[email protected]", address_id: 1611, add_date: null}}, 
{type:"customer", data:{customer_id: 36, store_id:193, first_name: "Kaitlyn", last_name: "Adams", email: "[email protected]", address_id: 5464, add_date: null}}, 
{type:"customer", data:{customer_id: 73, store_id:297, first_name: "Melissa", last_name: "Bennett", email: "[email protected]", address_id: 4536, add_date: null}},   
{type:"address", data:{address_id: 1023, address: "2895 Yonge St.",  city:"Toronto", province:"ON", postal_code:"L4C02G"}}, 
{type:"address", data:{address_id: 1984, address: "3611 Main St. West", city:"Hamilton", province:"ON", postal_code:"R5O8H5"}}, 
{type:"address", data:{address_id: 1757, address: "1177 Ontario St. Unit 8", city:"Mississauga", province:"ON", postal_code:"L9H6B3"}}, 
{type:"address", data:{address_id: 4536, address: "3945 John St.", city: "Ajax", province: "ON", postal_code: "L7M4T9"}}, 

var CustomerDB = 
{ 
    customer:[], 
    address:[], 
    store:[], 

    insertData:function (allData) 
    { 
     for (var i = 0; i < allData.length; i++) 
     {  
     if (allData[i].type == "store") 
      { 
      store[i].push(allData[i].store_id, allData[i].name, allData[i].address_id); 

      } 
     else if (allData[i].type == "customer") 
      { 
      this[i].add_date = new Date(); 
      customer[i].push(allData[i].customer_id, allData[i].store_id, allData[i].first_name, allData[i].last_name, allData[i].email, allData[i].addess_id, allData[i].add_date); 
      } 
     else if (allData[i].type == "address") 
      { 
      address[i].push(allData[i].address_id, allData[i].address, allData[i].city, allData[i].province, allData[i].cpostal_code); 
      } 
     } 
    } 
} 
CustomerDB.insertData(allData); 
+0

私は同じクエストに答えている気がします他の場所のイオン! –

+0

その別のエラー、 –

+0

ここで同じ正確な質問http://stackoverflow.com/questions/42181981/adding-elements-from-one-array-to-another/42182030#42182030 –

答えて

0

まだ私はhttps://stackoverflow.com/a/42182030/6647153が最善の答えだと思いますが。しかし、ここであなたが行く:

  1. これは大きな問題です:あなたは、プロパティ(配列)storecutomeradressにアクセスするためにthisキーワードを使用する必要があります。
  2. 配列にプッシュするときは、添字を使用する必要はありません。
  3. オブジェクトをグループ化しておくために、オブジェクト内にデータをラップする必要があります。

    function clone(obj) { 
        var newObj = {}; 
        for(var key in obj) 
         newObj[key] = obj[key]; 
        return newObj; 
    } 
    
    :あなたがオブジェクトをコピーすると、元のを押していない場合、このように、この機能を使用する

    insertData: function (allData) { 
        for (var i = 0; i < allData.length; i++) {  
         if (allData[i].type == "store") { 
          this.store.push(allData[i]); 
         } 
         else if (allData[i].type == "customer") { 
          allData[i].add_date = new Date(); 
          this.customer.push(allData[i]); 
         } 
         else if (allData[i].type == "address") { 
          this.adress(allData[i]); 
         } 
        } 
    } 
    

はこれを試してみてください(ここで私は、元のオブジェクトを使用しました)

それをこのように呼んでください:

insertData: function (allData) { 
    for (var i = 0; i < allData.length; i++) { 
     var o = clone(allData[i]); 
     var type = o.type; 
     delete o.type; // to remove the type if you want 
     if (type == "store") { 
      this.store.push(o); 
     } 
     else if (type == "customer") { 
      var o.add_date = new Date(); 
      this.customer.push(o); 
     } 
     else if (type == "address") { 
      this.adress(o); 
     } 
    } 
} 
+0

お世話になりました。あなたが他の方法を使用したくない理由は、私がまだそれを学んでいないということでした。だから、elsesのコードを取り入れて、それが働いたからといって私の中に入れたいだけではありません。私は自分のソリューションを思いついて実行し、見つけたエラーを修正したいと思っています。なぜなら、それは私が最もよく学ぶ方法なのです。 :) PS、アドレスをプッシュするときに.pushを忘れたと思います –

+0

あなたの徹底は非常に高く評価されています –

+0

@WorkMan [** MDN **](https://developer.mozilla。 org/en-US/docs/Web/JavaScript /リファレンス/オペレータ/ this) [**オブジェクトメソッド**]セクションをチェックしてください! –

関連する問題