Setting a value in an array

Silver Contributor
Silver Contributor

Background:

I have a variable "cruise_id" that is populated in our data layer as an array. However, it only ever has one item.

We recently built new landing pages and those pages do not populate the cruise_id in any form...(a new vendor built the pages and long story short...they don't have the id's to properly populate the variable at this time). 

I need to use an extension to set the cruise_id for these specific pages. I originally used a the lookup table and set the variale type to "array", but it did not populate at all. I additionally tried to wrap the numeric string in square brackets...and also in quotes. I ultimately did get it to populate, but it ended up populating as a string "["1111"]".

 

I then attempted to write it in JS but stil could not get it to populate as an array. Code below (edited out actual pathname and cruise_ids)

 

if (b['dom.pathname'].indexOf("page1") >-1)
    {
        b.cruise_id[0] = "1111";
    }
else if (b['dom.pathname'].indexOf("page2") >-1)
    {
        b.cruise_id[0] = "2222";
    }
else if (b['dom.pathname'].indexOf("page3") >-1)
    {
        b.cruise_id[0] = "3333";
    }
else if (b['dom.pathname'].indexOf("page4") >-1)
    {
        b.cruise_id[0] = "4444";
    }
else if (b['dom.pathname'].indexOf("page5") >-1)
    {
        b.cruise_id[0] = "5555";
    }
else if (b['dom.pathname'].indexOf("page6") >-1)
    {
        b.cruise_id[0] = "6666";
    }
else if (b['dom.pathname'].indexOf("page7") >-1)
    {
        b.cruise_id[0] = "7777";
    }

Any recommendations?

 

Michael

2 REPLIES 2

Setting a value in an array

Tealium Employee
Hi Michael, try this instead in your if blocks:
b.cruise_id = b.cruise_id || [];
b.cruise_id.push("6666");

Kind regards,
Kevin

Setting a value in an array

Silver Contributor
Silver Contributor

Thank you thank you! 

It worked! 

 

Public