Search code examples
javascriptnode.jstypescriptobjectplaywright

How to use if else statement in objects?


How to change below code to object structure. My aim is here I want to access nodID and xID keys from different files.But the problem is in below code there is a if statement.So I thought that if I use export const testConfig ={} and import testConfig from any file I can access to nodID and xId. But I don t know how can convert below code a object structure and access nodID and xID keys from different files.

    var env = process.env.ENV;
    if (!env) {
          env= "1";
       }
    if (env == '1') {
      nodID= 'aaaaaaa-f517-4859-91ff-bbbbbbbbb',
      xID='cccccc-f517-4859-91ff-nnnnnnnn'
     },
    else if (env == '2') {
      nodeID='cacacaca-7c96-441d-a3af-hththt'
      
    }
    else{
      nodeID='mlmlmlml-7c96-441d-a3af-frfrrfrrf'
    }

I would like to know if I have a different access option instead of the object structure?


Solution

  • Does something like this suit you ? or may be you want to edit your process.env to enrich new value ?

    // Create a proces.env.ENV variable in this snippet
    const process = { env: { ENV: '1' } };
    // Extract `ENV` from process.env and create a variable with this value called `env`
    const { ENV: env } = process.env;
    const toto = {
      ...((!env || env === '1') && {
        nodID: 'aaaaaaa-f517-4859-91ff-bbbbbbbbb',
        xID: 'cccccc-f517-4859-91ff-nnnnnnnn'
      })
    };
    console.log(toto);