Files
30-seconds-of-code/snippets/mapObject.md
Angelos Chalaris 5b2269be8d Update mapObject.md
2020-04-15 18:31:58 +03:00

614 B

title, tags
title tags
mapObject array,object,intermediate

Maps the values of an array to an object using a function, where the key-value pairs consist of the original value as the key and the result of the function as the value.

Use Array.prototype.reduce() to apply fn to each element in arr and combine the results into an object. Use el as the key for each property and the result of fn as the value.

const mapObject = (arr, fn) => 
  arr.reduce((acc, el, i) => {
    acc[el] = fn(el, i, arr);
    return acc;
  }, {});
mapObject([1, 2, 3], a => a * a); // { 1: 1, 2: 4, 3: 9 }