Files
30-seconds-of-code/snippets/objectFromPairs.md
Isabelle Viktoria Maciohsek 27c168ce55 Bake date into snippets
2021-06-13 13:55:00 +03:00

425 B

title, tags, firstSeen, lastUpdated
title tags firstSeen lastUpdated
objectFromPairs object,array,beginner 2017-12-17T17:55:51+02:00 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.
const objectFromPairs = arr =>
  arr.reduce((a, [key, val]) => ((a[key] = val), a), {});
objectFromPairs([['a', 1], ['b', 2]]); // {a: 1, b: 2}