Object from pairs

OSCC · JavaScript, Object, Array · Oct 21, 2020

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}

Recommended snippets

  • Object to entries

    JavaScript, Object

    Creates an array of key-value pair arrays from an object.

  • Object to pairs

    JavaScript, Object

    Creates an array of key-value pair arrays from an object or other iterable.

  • Object to pairs

    JavaScript, Object

    Creates an array of key-value pair arrays from an object.