Skip to content

Instantly share code, notes, and snippets.

@benadamstyles
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benadamstyles/7cc0f09ab241b2740e53 to your computer and use it in GitHub Desktop.
Save benadamstyles/7cc0f09ab241b2740e53 to your computer and use it in GitHub Desktop.
const keypaths = [];
const values = [];
const mapper = (input, keypath = '') => {
if (!_.isObject(input)) {
keypaths.push(keypath);
values.push(input);
} else {
const isArray = Array.isArray(input);
if (keypath) {
if (isArray) keypath += '[';
else keypath += '.';
};
_.each(input, (val, key) => {
const base = keypath + key + (isArray ? ']' : '');
if (_.isObject(val)) {
if (Array.isArray(val)) {
val.forEach((v, i) => {
mapper(v, `${base}[${i}]`);
});
} else {
_.each(val, (v, k) => {
mapper(v, `${base}.${k}`);
});
}
} else {
keypaths.push(base);
values.push(val);
}
})
}
};
mapper(obj);
return _.object(keypaths, values)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment