Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!