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

189
Views
How to delete an array attribute for all values in node js?

I want to return all users in JSON format except their passwords. To do so, I have done something like this

try {
        const rawResults = await model.find(query).limit(limit).skip(startIndex)
        results.results = rawResults.forEach((v) => delete v.password)
    } catch (error) {
        results.error = error
    }

now when I am trying to console.log the result value, it always returns

{ next: null, results: undefined }

Please help me fix this issue. Thank you so much.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

ForEach modifies the original object.

If you don't need rawResults any more (hopefully, as passwords should be encrypted as Nick pointed out), you can use your code as follows:

try {
    const rawResults = await model.find(query).limit(limit).skip(startIndex)
    rawResults.forEach((v) => delete v.password)
    results.results = rawResults
} catch (error) {
    results.error = error
}

If you need separate RawResults and result objects you can use map instead of forEach:

try {
    const rawResults = await model.find(query).limit(limit).skip(startIndex)
    results.results = rawResults.map(({password, ...v}) => v)
} catch (error) {
    results.error = error
}
about 4 years ago · Juan Pablo Isaza Report

0

Array.prototype.forEach returns undefined.

Just call rawResults.forEach((v) => delete v.password) without the results.results = and your code will work.

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!