1

Implementation of lodash pick

Function JavaScript 1 revisions 65 a month ago a month ago
var pick = (obj, props) => (Array.isArray(props) ? props : [props]).reduce((a, b) => {
    const parts = b.split(".");
    let value = obj;

    for(const part of parts) {
        const match = part.match(/(.+?)\[(\d+)\]/);

		b = match ? match[1] : part;
        value = value[b];
        if(typeof value === "undefined") break;

        if(match) value = value[match[2]];
        if(typeof value === "undefined") break;
    };

    a[b] = value;
    return a;
}, {});

Lodash is dumb, not an exact implementation