Javascript Js不带引用地从数组复制对象
我在将对象复制到数组时遇到问题。我认为这是一个参考问题 在我的程序中,我几乎没有数组。首先是DataFormMonth—它是包含月份数据的对象数组。第二个是包含products对象的products数组。产品具有属性Forecastary,即对象数组 此处代码:Javascript Js不带引用地从数组复制对象,javascript,Javascript,我在将对象复制到数组时遇到问题。我认为这是一个参考问题 在我的程序中,我几乎没有数组。首先是DataFormMonth—它是包含月份数据的对象数组。第二个是包含products对象的products数组。产品具有属性Forecastary,即对象数组 此处代码: this.allProducts.map(function (product) { var dataForMonth = data.filter(function (e) { r
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;
);
问题是每个更改的通道属性都有相同的值-它是上一个产品的值吗?
有人知道怎么修吗 似乎您要编辑
此中的每个产品
。所有产品
。因此,您希望向地图添加一个返回值。您还应该使用let
,以便在map函数中保留声明的变量范围,尽管我相信map
函数已经解决了这一问题。此外,您不必将this.allProducts重新分配给map函数调用。因此,您的答案应该如下所示:
this.allProducts=this.allProducts.map(函数(产品){
让dataForMonth=data.filter(函数(e){
返回e.dataId==product.productDataId;
});
设channelsformon=[];
设z={posId:product.sales\u plan\u pos\u id,arry:[]}
用于(活页每月的活页通道){
让结果=dataForMonth.filter(函数(e){
返回e.CHANNEL==sheetMonth.CHANNEL;
});
product.forecastArry[someId].channels=channelsformon;
退货产品;
)
听起来像是在重复为什么不回拨地图的fucniton返回任何东西??仍然是一样的:/