简体   繁体   中英

JSON value update dynamically with process.env [node.js]

process.env.ENVIRONMENT = dev2

Input JSON:

{
   "base": {
       "product1" : "dev1.awesomeproduct1.com",
       "product2" : "dev1.awesomeproduct2.com"
   }
}

Output JSON:

Based on the process.env.ENVIRONMENT the product urls should be updated dynamically

{
    "base": {
         "product1" : "dev2.awesomeproduct1.com",
         "product2" : "dev2.awesomeproduct2.com"
     }
}

Do I correctly understand that you want to replace the part of the domain names up to the dot with your process.env.ENVIRONMENT variable?

Then the following code should work:

for (key in myJSON.base) {
  myJSON.base[key] = myJSON.base[key].replace(/^[^.]+/, process.env.ENVIRONMENT);
}

Obviously, you will need to amend it if there are other fields than product<n> in the base object, or if you need to do a more complex replacement.

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