有没有一种方法,以消除在数据层对象?

问题描述 投票:0回答:1

有没有一种方法,以消除在数据层对象?由于在图片中看到,有两个“google_tag_params”对象。我只需要一个。首先推入为onload然后在JS调用数据层上的第二推。我需要推动第二所以在数据层中没有重复的“google_tag_params”之前除去推一个第一

enter image description here

dataLayer.push(
            {   "google_tag_params":google_tag_params, 
                "ecomm_prodid":document.getElementById('prodid').value,
                "ecomm_pagetype": document.getElementById('pageType').value,
                "ecomm_totalvalue": price.toFixed(2),
            });
javascript jquery google-adwords
1个回答
2
投票

使用阵列Filter

//es6
dataLayer = dataLayer.filter(x=>!x.google_tag_params);//removes any object
            // which has a key google_tag_params, guessing that its value won't be falsy
 // better use x => !(google_tag_params in x)
dataLayer.push(newGoogleTagParam);

//es5
dataLayer = dataLayer.filter(function (x) {
  return !x.google_tag_params;
});
dataLayer.push(newGoogleParam);
© www.soinside.com 2019 - 2024. All rights reserved.