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

205
Views
Filter an Array with fill and Map

I don't want to add a value 0 to the array as Object. It is getting mapped with newArray now. How can I fix that?

modus = {start: -1, end: 0}

Output newArray: 0: {value: -1, label: -1} 1: {value: 0, label: 0} Output newArray should be without the {value: 0, label: 0}

  const getOptions = (modus) => {
    console.log(modus.range.end + ' ' + modus.range.start)
    let data;
    if (modus.range.end < 0.00 ) {
    data = Array(modus.range.start - modus.range.end + 1).fill('').map((_, idx) => modus.range.end + idx)
    } else {
       data = Array(modus.range.end - modus.range.start + 1).fill('').map((_, idx) => modus.range.start + idx)
    }
    const newArray = data.map(item => ( { value: item, label: item } ));
      return newArray
  }
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You could omit zero values.

const
    getObject = item => ({ value: item, label: item }),
    getOptions = ({ range: { start, end } }) => {
        [start, end] = [Math.min(start, end), Math.max(start, end)];
        const hasZero = start <= 0 && end >= 0;

        console.log(start, end, hasZero);
        return Array.from(
            { length: end - start + !hasZero },
            _ => getObject(start++ || start++)
        );
    }

console.log(getOptions({ range: { start: 0, end: -1 } }));
console.log(getOptions({ range: { start: -1, end: 0 } }));
console.log(getOptions({ range: { start: -3, end: 3 } }));
console.log(getOptions({ range: { start: 2, end: 5 } }));
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 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!