Last active
June 29, 2017 08:33
-
-
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
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
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