Files
30-seconds-of-code/snippets/objectify.md
Angelos Chalaris 8a6b73bd0c Update covers
2023-02-16 22:24:28 +02:00

811 B

title, tags, author, cover, firstSeen
title tags author cover firstSeen
Map an array to an object array,object chalarangelo metro-tunnel 2023-02-04T05:00:00-04:00

Maps an object array to an object, using the provided mapping functions.

  • Use Array.prototype.reduce() to map the array to an object.
  • Use mapKey to map the keys of the object and mapValue to map the values.
const objectify = (arr, mapKey, mapValue = i => i) =>
  arr.reduce((acc, item) => {
    acc[mapKey(item)] = mapValue(item);
    return acc;
  }, {});
const people = [ { name: 'John', age: 42 }, { name: 'Adam', age: 39 } ];
objectify(people, p => p.name.toLowerCase());
// { john: { name: 'John', age: 42 }, adam: { name: 'Adam', age: 39 } }
objectify(
  people,
  p => p.name.toLowerCase(),
  p => p.age
);
// { john: 42, adam: 39 }