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

146
Vistas
How can we have a conditional deconstruction declaration in NodeJS

In my NodeJS project, I have a line like this:

const { fieldA, fieldB, fieldC, fieldD, fieldE, fieldF, ...rest } = originalObject;

fieldB, fieldC, fieldD, fieldE & fieldF are never used again in code, but as we know, the way the ES-6 deconstruction works, the rest object will be a new object without fieldsA-F. Therefore, later somewhere there is a dB operation, in a way so that fieldsA-F aren't upserted.

const updatedItem = await model.findOneAndUpdate({ fieldA }, rest, { upsert: true });

However, I want to conditionally include/ exclude fieldF in rest. I know it can be done on the reading side, but how do it in the assignment side?

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

0

You would need to create a new object and pass it in the db assignment phase

const {param1, param2, param3} = require('./config.json')
const reqParam1 = true
const data = {}
if(reqParam1) data[key] = param1 // define key
updateDb(data)

or even better require everything and use the delete operator to remove the non required keys

const config = {"param1": "hi", "param2": "hello"} //require("./config.json")
const reqParam1 = false
if(!reqParam1) delete config.param1
console.log(config)

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