admin管理员组文章数量:1418697
I would like to read all the environment variables key
and value
dynamically and form the array like below:
environment: [
{
name: 'DB_NAME',
value: 'myDatabase'
},
{
name: 'DB_USER_NAME',
value: 'admin'
},
{
name: 'DB_PASSWORD',
value: 'admin'
},
{
name: 'DB_TABLE_NAME',
value: 'myTable'
},
]
I'm able to print all the environment variables but I'm struggling to get the key and value and form the array dynamically - could someone please help me with this? Appreciate your help in advance. Thanks.
const env = require('dotenv').config();
var environmetList = process.env
console.log("printing env variables", environmetList)
const environment = [];
I would like to read all the environment variables key
and value
dynamically and form the array like below:
environment: [
{
name: 'DB_NAME',
value: 'myDatabase'
},
{
name: 'DB_USER_NAME',
value: 'admin'
},
{
name: 'DB_PASSWORD',
value: 'admin'
},
{
name: 'DB_TABLE_NAME',
value: 'myTable'
},
]
I'm able to print all the environment variables but I'm struggling to get the key and value and form the array dynamically - could someone please help me with this? Appreciate your help in advance. Thanks.
const env = require('dotenv').config();
var environmetList = process.env
console.log("printing env variables", environmetList)
const environment = [];
Share
Improve this question
asked Nov 14, 2021 at 21:36
Mike MarshMike Marsh
3955 silver badges16 bronze badges
1
-
What is
process.env
and what happened toenv
? Isprocess.env
an object containing the system environment variables or an array of objects with settings? – jabaa Commented Nov 14, 2021 at 21:45
1 Answer
Reset to default 7Using Object#entries
, get the list of pairs in process.env
. Then, iterate over it using Array#map
to get a list of objects with name
and value
:
Object.entries(process.env).map(([key, value]) => ({ name: key, value }))
本文标签: javascriptHow to read environment variable key and value dynamically in NodeJSStack Overflow
版权声明:本文标题:javascript - How to read environment variable key and value dynamically in NodeJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745295018a2652023.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论