I'm struggling to insert data into an array within an object. The array consists of objects, as seen in the code below.
The main part of the code is functioning properly, it properly splits my payload. However, the main ecommObj is not displaying the array correctly.
I have tried using concat, push, push.apply and even leaving an "=" in there. I am running out of ideas.
var siteMerch = [
// List of site merch here
];
var payLoad = 5;
var del = "-_-";
var chunk = [];
var promotions = siteMerch.map(function(i){
let promotion = {
// Promotion object details here
}
return promotion;
})
var ecommObj = {
event: 'EEC Promo View',
ecommerce: {
promoView: {
promotions: []
}
}
};
while (promotions.length) {
chunk = promotions.splice(0,payLoad);
ecommObj.ecommerce.promoView.promotions = chunk; //The issue is HERE.
console.log(ecommObj); //This will be a dataLayer push later on, I'm just checking it first
ecommObj.ecommerce.promoView.promotions.length = 0;
}
I'd like the
ecommObj.ecommerce.promoView.promotions
to hold payloads of 5 objects each, but currently, the console displays an empty array.