Categories
JavaScript Answers

How to convert an array of key-value tuples into an object with JavaScript?

Spread the love

To convert an array of key-value tuples into an object with JavaScript, we use the Object.fromEntries method.

For instance, we write

const arr = [
  ["cardType", "iDEBIT"],
  ["txnAmount", "17.64"],
  ["txnId", "20181"],
];

console.log(Object.fromEntries(arr));

to call Object.fromEntries with arr to convert the arr key-value pair arrays into properties of the object returned.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *