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

456
Views
Why map is returning [undefined]? and how can i solve that?

I want to filter of my snapshot but i sometimes i am getting [undefined], i dont want to return undefined inside, what is the issue?

return snapshot.docs.map((doc) => {
  const jn = JSON.parse(doc.data().jsonData)
  const res=  jn.attributes.find(t => t.typet === tokenAttrs[0].name);
  if(res){
    return doc.data()
  }
}) 
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Many ways to solve it. One would be to use a foreach and push the new value to an new array. or you filter the empty values from your result before you send it back. like that:

const r = snapshot.docs.map((doc) => {
      const jn = JSON.parse(doc.data().jsonData)
      const res=  jn.attributes.find(t => t.typet === tokenAttrs[0].name);
      if(res){
        return doc.data()
      } else {
        return null;
      }
    }) 
    
// then remove all empty values
return r.filter(n => n)

with forEach()

const res = []; 
snapshot.docs.forEach((doc) => {
  const jn = JSON.parse(doc.data().jsonData)
  const res=  jn.attributes.find(t => t.typet === tokenAttrs[0].name);
  if (res){
    res.push(doc.data());
  }
}) 

return res;
about 4 years ago · Juan Pablo Isaza Report

0

May it can't find the attribute that match the condition,cause the res is undefined. You should check it out.

about 4 years ago · Juan Pablo Isaza Report

0

You can use the filter function this way:

return snapshot.docs.map(doc => {
    const jn = JSON.parse(doc.data().jsonData);
    const res = jn.attributes.find(t => t.typet === tokenAttrs[0].name);

    if (res) return doc.data();
}).filter(_ => _ !== undefined);

This will remove the undefined values from the array.

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!