2017-02-18 42 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. 這是主要的問題:你必須使用this關鍵字來訪問屬性(陣列)storecutomeradress
  2. 推送到數組時,不需要使用下標。
  3. 您必須將數據包裝在對象中才能將事物分組。 (這裏我用的是原來的對象)

試試這個:如果你要複製的對象

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

,而不是推原,然後使用此功能是這樣的:

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++) { 
     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

感謝您的耐心等待。我不想使用其他方法的原因是,我還沒有學到這一點,所以我不只是想把別人的代碼放到我的手中,而只是因爲它工作。我想提出我自己的解決方案並執行它並修復任何我發現的錯誤,因爲這就是我學習最好的方法。 :) PS,我認爲你在推送地址時忘了一個.push –

+0

你的徹底性非常感謝 –

+0

@WorkMan這裏是[** MDN **](https://developer.mozilla)上的'this'的文檔。組織/ EN-US /文檔/網絡/的JavaScript /參考/運營/本)!檢查部分[**作爲對象方法**]! –