Skip to content

Instantly share code, notes, and snippets.

@emilioriosvz
Last active June 29, 2017 08:33
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 emilioriosvz/0e328ff408f1b194e019958fee477d94 to your computer and use it in GitHub Desktop.
Save emilioriosvz/0e328ff408f1b194e019958fee477d94 to your computer and use it in GitHub Desktop.
function that receive an object and change the values by the type of the key
var o = {
'1': 'adios',
'2': 1.5,
'3': true,
'4': [1, 2, 3],
'5': {1: 2}
}
const getTypes = obj => {
return Object.keys(obj).reduce((prev, key) => {
prev[key] = Array.isArray(obj[key]) ? 'array' : typeof obj[key]
return prev
}, {})
}
console.log(getTypes(o))
// { '1': 'string',
// '2': 'number',
// '3': 'boolean',
// '4': 'array',
// '5': 'object' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment