itemOmit β GTM Variable Template for Items
itemOmit EXTENDED Items
Removes specified properties from each object in an array.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβRemove single property
INPUT
Source Array: [
{name: 'Shirt', price: 10, internalId: 'abc'},
{name: 'Pants', price: 20, internalId: 'def'}
]
Properties to Remove: [{value: 'internalId'}]
{name: 'Shirt', price: 10, internalId: 'abc'},
{name: 'Pants', price: 20, internalId: 'def'}
]
Properties to Remove: [{value: 'internalId'}]
OUTPUT
[ {name: 'Shirt', price: 10}, {name: 'Pants', price: 20} ]
Remove multiple properties
INPUT
Source Array: [
{name: 'Shirt', price: 10, cost: 5, internalId: 'abc'},
{name: 'Pants', price: 20, cost: 8, internalId: 'def'}
]
Properties to Remove: [{value: 'cost'}, {value: 'internalId'}]
{name: 'Shirt', price: 10, cost: 5, internalId: 'abc'},
{name: 'Pants', price: 20, cost: 8, internalId: 'def'}
]
Properties to Remove: [{value: 'cost'}, {value: 'internalId'}]
OUTPUT
[ {name: 'Shirt', price: 10}, {name: 'Pants', price: 20} ]
Live Sandbox
Section titled βLive SandboxβThis is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
itemOmit
Source Array
πΎ The array of objects to process.
Supported formats:
β Items variable: {{ecommerce.items}}
β Array literal: [{name: "Shirt", price: 10, internalId: "abc"}]
Supported formats:
β Items variable: {{ecommerce.items}}
β Array literal: [{name: "Shirt", price: 10, internalId: "abc"}]
Properties to Remove
πΎ The property name to remove from each item (e.g., "internalId", "cost", "_private").
*** Remove single property***
*** Remove multiple properties***
*** Remove single property***
*** Remove multiple properties***
β
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the source array before processing.
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the resulting array before returning it (e.g.,
arr => arr.filter(x => x.price > 0) to keep only items with a positive price).Source Array array
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
Properties to Remove list
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
itemOmit()
Related Variables
Section titled βRelated VariablesβSame category: Items
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Creates a new array of objects with specified properties removed from each item. * Preserves all other existing properties. * * @param {Array} data.src - The array of objects to process. * @param {Array} data.kys - Array of objects with property names to remove. * @param {Function|string} [data.out] - Optional output handler. * * Direct-mode specific parameters: * @param {Function} [data.pre] - Optional pre-processor function to transform src before processing. * * @returns {Array} A new array of objects with the specified properties removed. * * @framework ggLowCodeGTMKit */const getType = require('getType');const createFlatArrayFromValues = function(list, property) { const result = []; if (!list) return result; for (let i = 0; i < list.length; i++) { const val = list[i][property]; if (getType(val) === 'array') { for (let j = 0; j < val.length; j++) { result.push(val[j]); } } else if (val) { result.push(val); } } return result;};const itemOmit = function(arr, keys) { if (getType(arr) !== 'array') return []; if (!keys || keys.length === 0) return arr;
const omitMap = {}; for (let i = 0; i < keys.length; i++) { omitMap[keys[i]] = true; }
return arr.map(function(item) { if (item == null || typeof item !== 'object') return item; const result = {}; for (const k in item) { if (item.hasOwnProperty(k) && !omitMap[k]) { result[k] = item[k]; } } return result; });};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// itemOmit - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);const keys = createFlatArrayFromValues(data.kys, "value");return out(itemOmit(value, keys));// ===============================================================================// itemOmit(...) β Apply Mode// ===============================================================================/*return function(arr) { const keys = createFlatArrayFromValues(data.kys, "value"); return out(itemOmit(arr, keys));};*/π§ͺ View Test Scenarios (6 tests)
β
'[example] Remove single property'β
'[example] Remove multiple properties'β
Non-existent property is ignoredβ
Empty keys returns original arrayβ
Invalid src returns empty arrayβ
Preserves items with null values for non-omitted properties