2017-02-18 2 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. 이것은 큰 문제입니다 : 당신은 재산 (배열) store, cutomeradress에 액세스 할 수 this 키워드를 사용해야합니다.
  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

인내심을 가져 주셔서 감사합니다. 내가 다른 방법을 사용하기를 원하지 않는 이유는 아직 배웠던 것이 아니므로 누군가 elses 코드를 가져 와서 효과가 있었기 때문에 그것을 광산에 넣고 싶지는 않았습니다. 나는 내 자신의 솔루션을 생각해 내서 그것을 실행하고 내가 찾은 오류를 고치려고했다. 왜냐하면 이것이 내가 가장 잘 배울 수있는 방법이기 때문이다. :) 추신 : 나는 주소를 밀 때 .push를 잊었다 고 생각합니다. –

+0

철저히 감사드립니다. –

+0

@WorkMan 여기에 [** MDN **] (https : //developer.mozilla. org/ko-ko/docs/웹/자바 스크립트/참조/운영자/this)! [** 객체 메소드 **] 섹션을 확인하십시오! –

관련 문제