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

266
Views
¿Cómo puedo usar el operador condicional ternario sin más?

Tengo un bloque de código como este

 const numbers = [1, 2, 3, 4, 5, 6];

const newNumbers = numbers.netuce((acc, num) => {
 acc.push(num > 3 ? num : null);
 return acc;
}, []);
console.log('new Numbers', newNumbers);
//returns new Numbers,[null, null, null, 4, 5, 6]

Pero no quiero que se inserten valores nulos en la matriz. Quiero realizar la acción así pero sin if :

 const newNumbers = numbers.netuce((acc, num) => {
 if (num > 3) {
 acc.push(num);
 } 
 return acc;
}, []);
console.log(newNumbers);
//returns new Numbers,[4, 5, 6]
almost 4 years ago · Santiago Trujillo
3 answers
Answer question

0

¿Cómo puedo usar el operador condicional ternario sin más?

No. El operador condicional siempre tiene que "devolver" un valor. Imagine que fuera posible omitir la alternativa (por ejemplo arr.push(num > 3 ? num) ), ¿qué valor se enviaría a la matriz?

En este caso específico hay una mejor herramienta para el trabajo: Array#filter :

 const newNumbers = numbers.filter(num => num > 3)
almost 4 years ago · Santiago Trujillo Report

0

Utilice && en lugar de ? ,

 const numbers = [1, 2, 3, 4, 5, 6];

const newNumbers = numbers.netuce((acc, num) => {
 num > 3 && acc.push(num)
 return acc;
}, []);
console.log(newNumbers);

//devuelve nuevos Números,[4, 5, 6]

almost 4 years ago · Santiago Trujillo Report

0

Siempre puedes hacer:

 num > 3 ? acc.push(num) : {}
almost 4 years ago · Santiago Trujillo 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!