代码之家  ›  专栏  ›  技术社区  ›  forbidden

如何在Javascript中提取大量数组形式的env变量?复制

  •  0
  • forbidden  · 技术社区  · 2 年前

    我有超过10个API帐户和密钥存储在 .env

    我的.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"
    ...
    

    我想实现但不想实现的目标:

    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 回复  |  直到 2 年前
        1
  •  1
  •   Nicklaus Brain    2 年前
    let accountInfo = []
    
    for (let i = 1; i < 6; i++){
        accountInfo.push([process.env[`API_ACCOUNT_${i}`], process.env[`API_SECRET_${i}`]])  
    }
    
    

    请尝试

    推荐文章