freeCodeCamp Challenge Guide: Hash from two arrays

Hash from two arrays


Solutions

Solution 1 (Click to Show/Hide)
function arrToObj(keys, vals) {
  let hash = {};
  for (let i = 0; i < keys.length; i++) {
    hash[keys[i]] = vals[i];
  }
  return hash;
}
Solution 2 (Click to Show/Hide)
function arrToObj(keys, vals) {
  return keys.reduce(
    (hash, key, index) => {
      hash[key] = vals[index];
      return hash;
    }, {})
  }
1 Like