2017-05-12 43 views
0

我有一個將對象複製到數組的問題。我認爲這是一個參考問題。Js從陣列中複製對象而無需參考

在我的程序中,我有幾個數組。首先是dataForMonth - 它是包含月份數據的對象數組。其次是包含產品對象的產品數組。產品具有屬性預測設置對象數組。

下面的代碼:

this.allProducts.map(function (product) { 

      var dataForMonth = data.filter(function (e) { 
       return e.dataId === product.productDataId; 
      }); 

      var z = { posId: product.sales_plan_pos_id, arry: [] } 
      for (var sheetMonth of sheet.channels) { 
       var result = dataForMonth.filter(function (e) { 
        return e.CHANNEL === sheetMonth.CHANNEL; 
       }); 

      product.forecastArry[someId].channels = result; 
); 

的問題是,每一個改變信道特性具有相同的價值 - 它從去年的產品價值? 有人知道如何解決它?

+0

聽起來像一個dup http://stackoverflow.com/questions/28150967/typescript-cloning-object –

+0

爲什麼不回電話funer返回任何東西? –

回答

0

好像你想要編輯中的每個product。所以你想添加一個返回值到你的地圖。您還應該使用let,以便聲明的變量範圍保留在map函數中,但我相信map函數已經考慮到了這一點。另外,不是你必須將this.allProducts重新分配給你的map函數調用。所以,你的回答應該是類似以下內容:

this.allProducts = this.allProducts.map(function (product) { 
 

 
    let dataForMonth = data.filter(function (e) { 
 
     return e.dataId === product.productDataId; 
 
    }); 
 

 
    let channelsForMont = []; 
 
    let z = { posId: product.sales_plan_pos_id, arry: [] } 
 
    for (let sheetMonth of sheet.channels) { 
 
     let result = dataForMonth.filter(function (e) { 
 
      return e.CHANNEL === sheetMonth.CHANNEL; 
 
     }); 
 

 
product.forecastArry[someId].channels = channelsForMont; 
 
return product; 
 
);

P.S你原來的代碼有一些失蹤的支架和result變量是未使用的。你應該對他們做些事情。

+0

仍然一樣:/ –