Files
30-seconds-of-code/javascript/snippets/object-from-pairs.md
2023-05-01 22:35:56 +03:00

584 B

title, type, tags, cover, dateModified
title type tags cover dateModified
Object from pairs snippet
object
array
silver-flat-screen 2020-10-21T21:54:53+03:00

Creates an object from the given key-value pairs.

  • Use Array.prototype.reduce() to create and combine key-value pairs.
  • Object.fromEntries() provides similar functionality.
const objectFromPairs = arr =>
  arr.reduce((a, [key, val]) => ((a[key] = val), a), {});
objectFromPairs([['a', 1], ['b', 2]]); // {a: 1, b: 2}