How to extract a lot of env variable in terms of array in Javascript?

63 views Asked by At

I have more than 10 API account and keys stored in .env

My .env:

API_ACCOUNT_1="11111"
API_SECRET_1="12121"

API_ACCOUNT_2="22222"
API_SECRET_2="232323"

API_ACCOUNT_3="33333"
API_SECRET_3="343434"

API_ACCOUNT_4="44444"
API_SECRET_4="454545"
...

What I want to achieve but not working:

import dotenv from "dotenv"

dotenv.config()

let accountInfo = []

for (let i = 1; i < 6; i++){
    // it is not the correct syntax
    accountInfo.push([process.env.`'API_ACCOUNT_${i}'`, process.env.`'API_SECRET_${i}'`])  
}
1

There are 1 answers

0
Nicklaus Brain On BEST ANSWER
let accountInfo = []

for (let i = 1; i < 6; i++){
    accountInfo.push([process.env[`API_ACCOUNT_${i}`], process.env[`API_SECRET_${i}`]])  
}

Please try