emilioriosvz
6/28/2017 - 4:36 PM

function that receive an object and change the values by the type of the key

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' }