简体   繁体   中英

Parse a string to array in node.js

I am trying to wrap my Node.js app with Docker. One of the things I need to get from the image runner is an environment variable that contains an array.

For example:

docker run -d -e DM_HOSTS=['localhost:1234', 'localhost'] someimage...

And my code:

let hosts:string[] = process.env.DM_HOSTS ? process.env.DM_HOSTS : ['localhost:1234'];

The problem is - I need to parse this environment variable to a real JS array.

I tried JSON.parse() and Array.from() and it didn't work.

Appriciate your help!

您需要传递有效的JSON,然后可以使用JSON.parse(),

docker run -d -e DM_HOSTS='["localhost:1234", "localhost"]' someimage...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM