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