How to create an object from the pairs of key and value in JavaScript

In this Article we will go through how to create an object from the pairs of key and value only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.

Let's define this short function:

const toObj = arr => Object.fromEntries(arr)

#Or

const toObj = arr => arr.reduce((a, c) => ((a[c[0]] = c[1]), a), {});

#Example

toObj([['a', 1], ['b', 2], ['c', 3]]);      // { a: 1, b: 2, c: 3 }