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

103
Views
¿Cómo soltar elementos nulos pero no indefinidos cuando se usa `arr.flatMap((f) => f ?? [])`?

He escrito una función que elimina valores null de una matriz:

 const dropNull = <T,>(arr: T[]): T[] => { return arr.flatMap((f) => f ?? []); // depends >= ES2019 };

Por ejemplo:

 const myArr1 = ['foo', 'bar', 'baz', null] const output1 = dropNull(myArr1) console.log(output1) // => ["foo", "bar", "baz"]

Sin embargo, me di cuenta de que también elimina valores undefined .

 const myArr2 = ['foo', 'bar', 'baz', null, undefined] const output2 = dropNull(myArr2) console.log(output2) // => ["foo", "bar", "baz"]

¿Hay alguna manera de simplemente modificar el dropNull() actual para eliminar null pero no undefined ? Es decir, sé que podría haber reescrito la función como:

 const dropNull2 = <T,>(arr:T[]): T[] => { return arr.filter(element => element !== null) }

Pero me gusta el arr.flatMap((f) => f ?? []) . ¿Hay un pequeño cambio en él para que solo caiga null pero no undefined ?

parque infantil TS

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

0

Puede utilizar los operadores ternarios en su lugar

 return arr.flatMap((f) => f === null ? [] : f);
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!