Skip to content

Instantly share code, notes, and snippets.

@ligne13
Last active January 11, 2019 12:53
Show Gist options
  • Save ligne13/9f610a281105d436aaba01427995d562 to your computer and use it in GitHub Desktop.
Save ligne13/9f610a281105d436aaba01427995d562 to your computer and use it in GitHub Desktop.
pickDeep function (like lodash pick function but with recursivity). Demo : https://jsbin.com/demaveq/edit?js,console
var test = {
key1: {
key11: 'test11',
key13: 'test13'
},
key2: 'test2'
};
var source = {
key1: {
key11: 'source11',
key12: 'source12'
},
key2: 'hello'
};
function pickDeep(collection, source) {
_.mapValues(collection, (value, key, object) => {
if (_.isObject(value)) {
// todo recursive
pickDeep(value, source[key])
} else {
if (typeof source[key] === "undefined") {
delete object[key];
} else {
return value;
}
}
});
return collection;
}
console.log(pickDeep(test, source));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment