Skip to content

itemFilterByProperty β€” GTM Variable Template for Items

VARIABLES β€Ί ITEMS
itemFilterByProperty EXTENDED Items
Direct (.tpl) Apply (.tpl)

Filters an array of items, keeping only the objects that match the specified property and value.


Filter by value
INPUT
Items (Array of Objects): [{cat: "A", v: 1}, {cat: "B", v: 2}]
Property Name: cat
Match Value (Optional): A
OUTPUT
[{cat: "A", v: 1}]
Filter with a predicate function
INPUT
Items (Array of Objects): [{name: "Shirt", price: 30}, {name: "Jacket", price: 120}, {name: "Hat", price: 15}]
Property Name: price
Match Value (Optional): x => x > 50
OUTPUT
[{name: "Jacket", price: 120}]

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

itemFilterByProperty
Items (Array of Objects)
πŸ’Ύ Array of objects to filter.

Supported formats:
  βœ“ Array
Property Name
πŸ’Ύ Property name to check in each object.

Supported formats:
  βœ“ String
Match Value (Optional)
🎯 The value that the property must equal to be included, or a predicate function (e.g. x => x > 50).

Supported formats:
  βœ“ Any
  βœ“ Function
Input Setup
Input Function (optional)
βš™οΈ Optional pre-processing function applied to the input before internal logic (e.g., convert object to string, normalize case). Internal transformations such as case handling will still apply afterward.
Result Handling
Output Function (optional)
βš™οΈ Optional function to apply to the result before returning it (e.g., str => str + ' €', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
Items (Array of Objects) array
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
Property Name string
Match Value (Optional) any
itemFilterByProperty()


πŸ“œ View Implementation Code
/**
* Filters an array of objects (items) returning only those where the specified property matches the provided value or satisfies a predicate function.
*
* @param {Array} data.src - Array of objects to filter.
* @param {string} data.prp - Property name to check.
* @param {any|Function} [data.val] - The expected value of the property, or a predicate function (e.g. x => x > 50).
* @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} Filtered array of objects.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const itemFilterByProperty = function(items, property, predicate) {
if (getType(items) !== 'array') return [];
const safePredicate = function(val) {
return getType(val) === 'function' ? val : function(x) { return val === x; };
};
const check = safePredicate(predicate);
return items.filter(function(item) {
if (!item || item[property] === undefined) return false;
return check(item[property]);
});
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// itemFilterByProperty - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(itemFilterByProperty(value, data.prp, data.val));
// ===============================================================================
// itemFilterByProperty(...) - Apply Mode
// ===============================================================================
/*
return function(value, property, predicate) {
property = data.rp1 ? property : data.prp;
predicate = data.rp2 ? predicate : data.val;
return out(itemFilterByProperty(value, property, predicate));
};
*/
πŸ§ͺ View Test Scenarios (3 tests)
βœ… '[example] Filter by value'
βœ… '[example] Filter with a predicate function'
βœ… Empty array returns empty