Skip to content

pick — GTM Variable Template for Object

VARIABLES › OBJECT
pick EXTENDED Object

Returns a new object with only the specified keys.



Examples

Pick single property
INPUT
Source Object: {name: 'John', age: 30, city: 'Paris', country: 'France'}
Properties to Pick: [{value: 'name'}]
OUTPUT
John
Pick multiple properties
INPUT
Source Object: {id: 1, name: 'Product', price: 99, stock: 50, category: 'electronics'}
Properties to Pick: [{value: 'name'}, {value: 'price'}, {value: 'category'}]
OUTPUT
Product

GTM Configuration

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

pick
Source Object
💾 The source object to pick properties from.

Supported formats:
  ✓ Object variable: {{myObject}}
  ✓ Object literal
Properties to Pick
💾 List of property names to pick from the source object. Only properties that exist in the source will be included.

Supported formats:
  ✓ String: property name
  ✓ Array of strings: for multiple properties at once

*** Pick single property***

*** Pick multiple properties***
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the source object before picking properties (e.g., normalize object structure, parse JSON).
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result object before returning it (e.g., obj => JSON.stringify(obj), obj => Object.freeze(obj)). Useful for chaining transformations on the output.
Source Object object
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Properties to Pick list
pick()


Under the Hood

📜 View Implementation Code
/**
 * Creates a new object composed of selected properties from the source object.
 *
 * @param {Object} data.src - The source object.
 * @param {Array} data.kys - Array of objects with property names to pick.
 * @param {Function|string} [data.out] - Optional output handler: function to transform result or string with format.
 *
 * Direct-mode specific parameters:
 * @param {Function} [data.pre] - Optional pre-processor function to transform src before processing.
 * 
 * @returns {Object} A new object with only the selected properties.
 *
 * @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 pick = function(object, keys) {
    const result = {};
    
    if (object == null || typeof object !== 'object') {
        return result;
    }
    
    for (let i = 0; i < keys.length; i++) {
        const key = keys[i];
        if (object.hasOwnProperty(key)) {
            result[key] = object[key];
        }
    }
    
    return result;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// pick - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
const keys = createFlatArrayFromValues(data.kys, "value");
return out(pick(value, keys));
// ===============================================================================
// pick(...) – Apply Mode
// ===============================================================================
/*
return function(object, keys) {
   keys = data.rp1 ? createFlatArrayFromValues(data.kys, "value") : keys;
   return out(pick(object, keys));
};
*/
🧪 View Test Scenarios (5 tests)
✅ '[example] Pick single property'
✅ '[example] Pick multiple properties'
✅ Test picking non-existent properties
✅ Test with array of keys in value property
✅ Test with empty keys array