Skip to content

Instantly share code, notes, and snippets.

@Belrestro
Last active January 18, 2018 11:35
Show Gist options
  • Save Belrestro/02ef7f119119c0fd639dfced4526e74f to your computer and use it in GitHub Desktop.
Save Belrestro/02ef7f119119c0fd639dfced4526e74f to your computer and use it in GitHub Desktop.
const mapToType = (parent, depth = 2) => {
const map = (object, lvl = 0) => {
if (object instanceof Array) object = object[0];
const props = {};
for (let key in object) {
const prop = object[key];
let type = typeof prop;
if (type === 'object') {
type = prop instanceof Array ? 'array' : 'object';
if ((lvl + 1) < depth && prop !== null) {
props[key] = {};
props[key][type] = mapToType(prop, lvl + 1);
continue;
}
props[key] = prop === null ? null : type;
} else {
props[key] = type;
}
}
return props
}
return map(parent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment