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

185
Vistas
How to update the existing list value for specific key in NodeJS

I've wanted to update the specific key's value dynamically in NodeJS. Initially, this key USER_SAL has the value of 0, I would like to update this key USER_SAL value dynamically from USER_SAL -> 0 To USER_SAL -> 50000. Please find my code below:

const obj = {
    users: [{
        name: "USER_ID",
        value: "ASD32343D",
      },
      {
        name: "USER_NAME",
        value: "mark",
      },
      {
        name: "USER_ALT_ID",
        value: "DSDF3234",
      },
      {
        name: "USER_LOC",
        value: "NY",
      },
      {
        name: "USER_SAL",
        value: "0",
      },
    ],
  };
  const changes = {
    USER_ALT_ID: "myuser.altID", // changing to different key name
    USER_LOC: "myuser.loc", // changing to different key name
  };
  
  obj.users.forEach((o) => {
    if (changes[o.name]) o.name = changes[o.name];
  });
  
  console.log(obj.users);

Expected output:

[
  { name: 'USER_ID', value: 'ASD32343D' },
  { name: 'USER_NAME', value: 'mark' },
  { name: 'myuser.altID', value: 'DSDF3234' },
  { name: 'myuser.loc', value: 'NY' },
  { name: 'USER_SAL', value: '50000' }
]

I'm not very good at NodesJS, can someone please help how can we update the specific key's value. Appreciate your help in advance. Thanks!

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

0

Use map instead of forEach, map allows you to create a new array based on changes to an old one. See below:

const obj = {
    users: [{
        name: "USER_ID",
        value: "ASD32343D",
      },
      {
        name: "USER_NAME",
        value: "mark",
      },
      {
        name: "USER_ALT_ID",
        value: "DSDF3234",
      },
      {
        name: "USER_LOC",
        value: "NY",
      },
      {
        name: "USER_SAL",
        value: "0",
      },
    ],
  };
  const changes = {
    USER_ALT_ID: "myuser.altID", // changing to different key name
    USER_LOC: "myuser.loc", // changing to different key name
  };
  
  const newUsers = obj.users.map((o) => {
    if (changes[o.name]) o.name = changes[o.name];
    if(o.name === "USER_SAL") o.value = "50000";
    return o;
  });
  
  obj.users = newUsers;
  
  console.log(obj.users);

about 4 years ago · Juan Pablo Isaza Denunciar

0

const obj = {
    users: [{
        name: "USER_ID",
        value: "ASD32343D",
      },
      {
        name: "USER_NAME",
        value: "mark",
      },
      {
        name: "USER_ALT_ID",
        value: "DSDF3234",
      },
      {
        name: "USER_LOC",
        value: "NY",
      },
      {
        name: "USER_SAL",
        value: "0",
      },
    ],
  };
  const keyChanges = {
    USER_ALT_ID: "myuser.altID", // changing to different key name
    USER_LOC: "myuser.loc", // changing to different key name
  };
  const valueChanges = {
    USER_SAL: 5000,
  };

  obj.users = obj.users.map(x=>({ 
    name: keyChanges[x.name] || x.name,
    value: valueChanges[x.name] || x.value, 
  }));
  
  console.log(obj.users);
about 4 years ago · Juan Pablo Isaza Denunciar

0

I created a function that would change that value to whatever you wanted:

const dynSal = (arr, dynVal) => {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i].name === "USER_SAL") {
      arr[i].value = dynVal;
    }
  }
}

dynSal(obj.users, 5000);

You provide the function with the array of objects. It loops through those objects and changes the value if it happens up an object for which the name is "USER_SAL" JP

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