Home > OS >  How to update environment variable key name dynamically using NodeJS
How to update environment variable key name dynamically using NodeJS

Time:11-16

I'm reading the environment variables (Key & Value) dynamically and forming below array:

commands: [
    {
        name: 'PRODUCT_NAME',
        value: 'iPhone'
    },
    {
        name: 'PRODUCT_PRICE',
        value: '1232'
    },
    {
        name: 'PRODUCT_TYPE',
        value: 'Electronics'
    },
    {
        name: 'PRODUCT_ID',
        value: 'SKU29389438'
    },
    {
        name: 'LOG_ENABLED',
        value: 'TRUE'
    },    
]

I want to update the key name for these two properties dynamically PRODUCT_TYPE -> myapp.property.type.event and PRODUCT_ID -> myapp.property.product.enabled

Final output should look like this:

commands: [
    {
        name: 'PRODUCT_NAME',
        value: 'iPhone'
    },
    {
        name: 'PRODUCT_PRICE',
        value: '1232'
    },
    {
        name: 'myapp.property.type.event',
        value: 'Electronics'
    },
    {
        name: 'myapp.property.product.enabled',
        value: 'SKU29389438'
    },
    {
        name: 'LOG_ENABLED',
        value: 'TRUE'
    },    
]

Please find my product.js code below:

const commands = (Object.entries(process.env).map(([key, value]) => ({ name: key, value })))
console.log("commands : ", commands);

I'm new to Nodejs, can someone please help how can I update these two key dynamically and form the final array?

Your help would be greatly appreciated!

CodePudding user response:

1) You can just loop over and change the name as:

const obj = {
  commands: [
    {
      name: "PRODUCT_NAME",
      value: "iPhone",
    },
    {
      name: "PRODUCT_PRICE",
      value: "1232",
    },
    {
      name: "PRODUCT_TYPE",
      value: "Electronics",
    },
    {
      name: "PRODUCT_ID",
      value: "SKU29389438",
    },
    {
      name: "LOG_ENABLED",
      value: "TRUE",
    },
  ],
};

obj.commands.forEach((o) => {
  if (o.name === "PRODUCT_TYPE") o.name = "myapp.property.type.event";
  if (o.name === "PRODUCT_ID") o.name = "myapp.property.product.enabled";
});

console.log(obj.commands);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

2) You can also do as :

one-liner

obj.commands.forEach((o) => (o.name = changes[o.name] ?? o.name));

const obj = {
  commands: [{
      name: "PRODUCT_NAME",
      value: "iPhone",
    },
    {
      name: "PRODUCT_PRICE",
      value: "1232",
    },
    {
      name: "PRODUCT_TYPE",
      value: "Electronics",
    },
    {
      name: "PRODUCT_ID",
      value: "SKU29389438",
    },
    {
      name: "LOG_ENABLED",
      value: "TRUE",
    },
  ],
};
const changes = {
  PRODUCT_TYPE: "myapp.property.type.event",
  PRODUCT_ID: "myapp.property.product.enabled",
};

obj.commands.forEach((o) => {
  if (changes[o.name]) o.name = changes[o.name];
});

console.log(obj.commands);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related