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

151
Views
how to conditionally define an array and avoid to have a null as element?

let's say we have the following code:

var numberofZeroArray = [10, 10, 10, 10, 10];

and I want to add another number between the second and third elements conditionally using a toggle

var addMore = XXX() // XXX() return true or false

so that when addMore is true then the add a number 5

[10, 10, 5, 10, 10, 10]

if false, keep the original list unchanged:

[10, 10, 10, 10, 10]

I tried

var addMore = true;
var numberofZeroArray = [10, 10, addMore ? 5 : null 10, 10, 10];  // is OK when addMore is true
// result is [10, 10, 5, 10, 10, 10]

it doesn't meet the expected result when addMore is false:

var addMore = false;
var numberofZeroArray = [10, 10, addMore ? 5 : null 10, 10, 10]; 
// result is [10, 10, null, 10, 10, 10]

I could use spread operator to define separate arrays for the first two elements and last three elements, but it is not feasible to do when the array contains a lot number of element. so what's the most easy way to achieve it?

about 4 years ago · Juan Pablo Isaza
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!