adobe-client-data-layer icon indicating copy to clipboard operation
adobe-client-data-layer copied to clipboard

Clear Arrays by pushing an empty array

Open onyx-blackbird opened this issue 4 years ago • 3 comments

Let's start with this simple DataLayer object:

{
  page: {
    campaigns: ['campaign1', 'campaign2']
  }
}

Then I push this:

adobeDataLayer.push({
  page: {
    campaigns: []
  }
});

Expected Behaviour

adobeDataLayer.getState() returning this:

{
  page: {
    campaigns: []
  }
}

Actual Behaviour

adobeDataLayer.getState() returning this:

{
  page: {
    campaigns: ['campaign1', 'campaign2']
  }
}

Workaround

You need to push null to delete it before setting it to an empty array as described above.

adobeDataLayer.push({
  page: {
    campaigns: null
  }
});

The other way would be to first find out how many items there are in the array already and do a push like this:

adobeDataLayer.push({
  page: {
    campaigns: [null, null]
  }
});

onyx-blackbird avatar May 26 '21 14:05 onyx-blackbird

I'm also seeing this issue and with updating arrays in general. Attempting to update an existing array of: campaigns: ['camp-one', 'camp-two'] with an update of: campaigns: ['camp-one'] will still result in a datalayer with: campaigns: ['camp-one', 'camp-two'] when calling adobeDataLayer.getState()

Is this by design or a bug? I was expecting any update in general to be valid.

L-Triple-O avatar Feb 03 '22 20:02 L-Triple-O

Just curious about this issue, but would you mind describing the need to clear these arrays?

jm-adobe avatar Jun 28 '22 20:06 jm-adobe

@jm-adobe in a case of wanting clear an array or pass new information on an event to the dataLayer. This also seems to be a possibility described in the documentation yet overwrite may not work as expected? .. One important detail to clarify is what happens when pushing data to an already existing array: it will overwrite the data of the inner array.

L-Triple-O avatar Jun 29 '22 13:06 L-Triple-O

I can't believe this has not been fixed yet. The currently observed behavior is the complete opposite of what one would expect. If I assign a new value to an existing variable I expect that variable to reference the new value, not a combination of the new and previous value.

NikolaOroz avatar Oct 03 '24 14:10 NikolaOroz