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