I hava a problem with copy a objects to array. I think it is an problem with reference.
In my program i have few array. First is dataForMonth - it is array of objects with data for month. And second is an products array which contains products objects. Product have property forecastArry wchich is array of objects.
Here code :
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;
);
The problem is that the every changed channels property have the same value - its a value from last product? Anybody know how to fix it ?
Seems like you want to edit each product
in this.allProducts
. So you want to add a return value to your map. You should also use let
so that the scope of variables declared is preserved within map function, although I believe the map
function already takes care of that. In addition, not that you have to reassign this.allProducts to your map function call. So your answer should be something like the following:
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; );
PS Your original code has some missing brackets and result
variable is unused. You should do something about them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.