2017-05-12 42 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

仍然一样:/ –