Oreol Noumodong

Oreol Noumodong

Learner

Convert an array of strings of size two to an object in JavaScript

11 Aug, 2022 - 03:49 AM

Let's say we have an array of strings of size two in which we want one to be the key and the other the value. Our job is to form an object from this array.
Here is the sample array:
let arr = ["token", "eyJfcmFpbHMiOnsibWVzc2FnZSI6IlcxczBPRG"]
We want to turn into this:
const obj = {
            token:"eyJfcmFpbHMiOnsibWVzc2FnZSI6IlcxczBPRG"
         }
The full code will be:
let obj = {}; 
const arr = ["token", "eyJfcmFpbHMiOnsibWVzc2FnZSI6IlcxczBPRG"];
obj[arr[0]] = arr[1];

console.log(obj);
/* 
{
 token:"eyJfcmFpbHMiOnsibWVzc2FnZSI6IlcxczBPRG"
} 
*/

Let's work together!

Available for collaboration, or mentorship.

Build with Next.js, Tailwindcss, Express.js and Vercel