0

I'm trying to push data into an array within an object. The array is an array of objects (it's clearer in the code).

The main code works, it splits my payload correctly but the main ecommObj doesn't output the array correctly.

Please help.

I've tried using concat, push, push.apply and finally I've left an "=" in there. I'm just out of ideas.

  var siteMerch = [
  {"placement": "homepage","position": "T1","campaignName": "poloshirts","campaignStartDate": "12-08-19"},
  {"placement": "homepage","position": "T2","campaignName": "groomingbestsellers","campaignStartDate": "12-08-19"},
  {"placement": "homepage","position": "T3","campaignName": "journalhowtobuyasuitonline","campaignStartDate": "12-08-19"},
  {"placement": "homepage","position": "WN","campaignName": "newarrivals","campaignStartDate": "12-08-19"},
  {"placement": "homepage","position": "C1","campaignName": "fourwaystorunbetter","campaignStartDate": "15-08-19"},
  {"placement": "homepage","position": "M1","campaignName": "mrpcap8tshirts","campaignStartDate": "12-08-19"},
  {"placement": "homepage","position": "T1","campaignName": "poloshirts","campaignStartDate": "12-08-19"},
  {"placement": "homepage","position": "T2","campaignName": "groomingbestsellers","campaignStartDate": "12-08-19"},
  {"placement": "homepage","position": "T3","campaignName": "journalhowtobuyasuitonline","campaignStartDate": "12-08-19"},
  {"placement": "homepage","position": "WN","campaignName": "newarrivals","campaignStartDate": "12-08-19"},
  {"placement": "homepage","position": "C1","campaignName": "fourwaystorunbetter","campaignStartDate": "15-08-19"},
  {"placement": "homepage","position": "M1","campaignName": "mrpcap8tshirts","campaignStartDate": "12-08-19"}
  ];

  var payLoad = 5;
  var del = "-_-";
  var chunk = [];
  var promotions = siteMerch.map(function(i){
  let promotion = {
      'id':i.placement + del + i.campaignName + del + i.campaignStartDate + del + i.position,
      'name':i.campaignName + " - " + i.campaignStartDate,
      'creative':i.placement,
      'position':i.position
  }
  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 want my ecommObj.ecommerce.promoView.promotions to contain payloads of 5 objects in there but currently the console shows the output to have a blank array.

Nik S
  • 105
  • 1
  • 10
  • 1
    can you please clarify the desired output? – Peter Aug 16 '19 at 21:13
  • Yes, that is what I'm trying to do, the issue is my `ecommObj.ecommerce.promoView.promotions` array is blank when it outputs to the console each time. I'd expect to see this with the items in `chunk`, so ideally I'd see 3 console objects each with with the `ecommObj` `promotions` array populated – Nik S Aug 16 '19 at 21:30
  • Why are you using `+ del + ... + del`? You can use `[i.placement, i.capmaignName, ...].join(del)`. – Tarwirdur Turon Aug 16 '19 at 21:49

1 Answers1

0

Your code is working fine. You think otherwise because the array has changed by the time you retrieve it by digging through the object inspector in the console.

Replace

console.log(ecommObj);

with

console.log(ecommObj.ecommerce.promoView.promotions);

or

console.log(JSON.stringify(ecommObj));

and you'll see that it works.


By the way, promotions: [] and ecommObj.ecommerce.promoView.promotions.length = 0; are unneeded and can be removed.

ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Thanks ikegami, if I replace the `console.log` command with `dataLayer.push(ecommObj)` where `dataLayer` is another array, I notice that the `ecommObj` still has the `promotions` array set to the last array push and the `dataLayer` objects are all the same. Any idea how to resolve this? – Nik S Aug 16 '19 at 22:45
  • And I presume you use the same `dataLayer` and the same `ecommObj` every pass of the loop? Well, since you never change `ecommObj`, so you end up with multiple references to the same object. If you want different objects, you will need to create different objects. Move `var ecommObj = { ... };` into the loop. – ikegami Aug 16 '19 at 23:02