Last active
January 18, 2018 11:35
-
-
Save Belrestro/02ef7f119119c0fd639dfced4526e74f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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