Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
How to update environment variable key name dynamically using NodeJS

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!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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; }

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; }

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda