Skip to content

itemOmit — GTM Variable Template for Items

VARIABLES › ITEMS
itemOmit EXTENDED Items

Removes specified properties from each object in an array.



Examples

Remove single property
INPUT
Source Array: [
{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'}]
OUTPUT
[ {name: 'Shirt', price: 10}, {name: 'Pants', price: 20} ]

GTM Configuration

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"}]
Properties to Remove
💾 The property name to remove from each item (e.g., "internalId", "cost", "_private").

*** 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
itemOmit()


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