Menu

gitpiper

mapObject javascript Code Snippet in 2024

arrayobjectintermediate

Last Updated: 20 June 2024

Maps the values of an array to an object using a function.

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

javascript snippet similar to mapObject For You in June 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️