Skip to content

Instantly share code, notes, and snippets.

@tvler

tvler/pick.js

Last active Jan 12, 2018
Embed
What would you like to do?
Experimental notation for picking a subset of a js object
const getProxy = (target = {}, key) =>
target[key] || (target[key] = new Proxy({}, { get: getProxy }));
const fillPick = (proxy, fn, fromObject = fn(proxy)) =>
Object.assign(
{},
...Object.entries(proxy).map(([key, value]) => ({
[key]: Object.keys(value).length
? fillPick(value, fn, fromObject[key])
: fromObject[key],
})),
);
const pick = fn => fillPick(getProxy(), fn);
const object = { a: 1, b: 2, c: 3, d: { e: 4, f: 5 } };
const subset = pick(({ a, b, d: { e } }) => object);
console.log(subset); // { a: 1, b: 2, d: { e: 4 } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.