Search code examples
javascriptrecursionlodash

Recursively collect values for property using lodash


For a nested complex object or array, I would like to collect all values for a given property name. Example:

var structure = {
    name: 'alpha',
    array: [
        { name: 'beta' },
        { name: 'gamma' }
    ],
    object: {
        name: 'delta',
        array: [
            { name: 'epsilon' }
        ]
    }
};

// expected result:  [ 'alpha', 'beta', 'gamma', 'delta', 'epsilon' ]

It's obvious how to achieve this using plain JS, but: Is there any elegant, concise approach using lodash?

[edit] Current variant below. Nicer solutions welcome!

function getPropertyRecursive(obj, property) {
    var values = [];
    _.each(obj, function(value, key) {
        if (key === property) {
            values.push(value);
        } else if (_.isObject(value)) {
            values = values.concat(getPropertyRecursive(value, property));
        }
    });
    return values;
}

Solution

  • This can be done elegantly with the following mixin, which is a recursive version of _.toPairs:

    _.mixin({
        toPairsDeep: obj => _.flatMap(
            _.toPairs(obj), ([k, v]) =>
                _.isObjectLike(v) ? _.toPairsDeep(v) : [[k, v]])
    });
    

    then to get the result you want:

    result = _(structure)
        .toPairsDeep()
        .map(1)
        .value()
    

    If there are scalar properties other than name, you'll have to filter them out:

    result = _(structure)
        .toPairsDeep()
        .filter(([k, v]) => k === 'name')
        .map(1)
        .value()