itemFilterByProperty β GTM Variable Template for Items
itemFilterByProperty EXTENDED Items
Filters an array of items, keeping only the objects that match the specified property and value.
Examples
Section titled βExamplesβFilter by value
INPUT
Items (Array of Objects): [{cat: "A", v: 1}, {cat: "B", v: 2}]
Property Name: cat
Match Value (Optional): A
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
Property Name: price
Match Value (Optional): x => x > 50
OUTPUT
[{name: "Jacket", price: 120}]
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.
itemFilterByProperty
Items (Array of Objects)
πΎ Array of objects to filter.
Supported formats:
β Array
Supported formats:
β Array
Property Name
πΎ Property name to check in each object.
Supported formats:
β String
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
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
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
itemFilterByProperty()
Related Variables
Section titled βRelated VariablesβSame category: Items
Under the Hood
Section titled βUnder the Hoodβπ 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