I have the following function I want to run within Google Tag Manager but I am getting the following error:
This language feature is only supported for ECMASCRIPT_2015 mode or better: computed property.
Below is the function I am trying to run. How would I alter this to work with GTM? Thanks
var compactObject = function(data) {
if (typeof data !== 'object') {
return data;
}
return Object.keys(data).reduce(function(accumulator, key) {
var isObject = typeof data[key] === 'object';
var value = isObject ? compactObject(data[key]) : data[key];
var isEmptyObject = isObject && !Object.keys(value).length;
if (value === undefined || isEmptyObject) {
return accumulator;
}
return Object.assign(accumulator, {[key]: value});
}, {});
}
You main question appears to be:
How can I re-write
{[key]: value}
so that it is supported in older versions of JS
You can re-write the computed property syntax using the longer (legacy) version
var obj = {};
obj[key] = value;
return Object.assign(accumulator, obj);