Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear Arrays by pushing an empty array #125

Open
onyx-blackbird opened this issue May 26, 2021 · 3 comments
Open

Clear Arrays by pushing an empty array #125

onyx-blackbird opened this issue May 26, 2021 · 3 comments
Labels
bug Unexpected problem or unintended behavior that impairs normal functioning.

Comments

@onyx-blackbird
Copy link

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]
  }
});
@L-Triple-O
Copy link

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.

@jm-adobe
Copy link

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

@L-Triple-O
Copy link

@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.

@gabrielwalt gabrielwalt added the bug Unexpected problem or unintended behavior that impairs normal functioning. label Jul 29, 2022
@ghost ghost mentioned this issue Apr 29, 2024
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unexpected problem or unintended behavior that impairs normal functioning.
Projects
None yet
Development

No branches or pull requests

4 participants