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

157
Views
Get all the numbers between 0 and y just given the y

So given a number (ex: 30), get ALL the numbers between it and 0 and print it on the screen

In my case I can't use it with a for or while loops, because i'm already inside one and that's considered an unreachable loop

I need to display these numbers inside an <option></option>

I know I can use a range with a single input, but I think it's clearer with <option>

const TodoApp = () => {
     const items = [
        {
          id: "877635284991-9",
          name: "ex3",
          quantity: 12
        }
     ]

    return (
      <div>
        <div>
          <select>
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
            <option>5</option>
            <option>6</option>
            <option>7</option>
            <option>8</option>
            <option>9</option>
            <option>10</option>
            <option>11</option>
            <option>12</option>
          </select>
        </div>
      </div>
    )
}

ReactDOM.render(<TodoApp />, document.querySelector("#app"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="app"></div>

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

0

You could create an array and map the values to <option> elements.

const { useState } = React;

const arrayOfSize = (size, fn) => Array.from(new Array(size), (_, i) => fn(i));

const items = [{
  id: '877635284991-9',
  name: 'ex3',
  quantity: 12
}];

const TodoApp = (props) => {
  const { items } = props;
  const [selectedItem, setSelectedItem] = useState(items[0]);

  return (
    <div>
      <select>
        {
          arrayOfSize(selectedItem.quantity, i => (
            <option key={i + 1} value={i + 1}>{i + 1}</option>)
          )
        }
      </select>
    </div>
  );
};

ReactDOM.render(<TodoApp items={items} />, document.querySelector('#react'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="react"></div>

Here is a full example:

const { Fragment, useState } = React;

const arrayOfSize = (size, fn) => Array.from(new Array(size), (_, i) => fn(i));

const items = [{
  id: '877635284991-9',
  name: 'ex3',
  quantity: 12
}, {
  id: '877635284991-0',
  name: 'ex4',
  quantity: 6
}];

const QuantitySelector = (props) => {
  const { value } = props;
  return (
    <select>
      {
        arrayOfSize(value, i => (
          <option key={i + 1} value={i + 1}>{i + 1}</option>)
        )
      }
    </select>
  );
};

const TodoItem = (props) => {
  const { name, quantity = 0 } = props;
  return (
    <div>
      <span>{name}</span>
      <QuantitySelector value={quantity} />
    </div>
  );
};

const TodoItems = (props) => {
  const { items } = props;
  return (
    <Fragment>
      {
        items.map(item => (
          <TodoItem {...item} />
        ))
      }
    </Fragment>
  );
};

const TodoApp = (props) => {
  const { items = [] } = props;

  return (
    <div id="app">
      <TodoItems items={items} />
    </div>
  );
};

ReactDOM.render(<TodoApp items={items} />, document.querySelector('#react'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script>
<div id="react"></div>

about 4 years ago · Juan Pablo Isaza Report

0

You can create arrays of required size using Array.from (MDN doc).

Array.from({length:3}) will create [undefined,undefined,undefined].

The 2nd optional argument of Array.from is a map callback, whose 2nd argument (index) can be used for your scenario.

Try something like:

Array.from({length: +items.quantity}, (element, index)=> {
    return index; // add option tag here
});
about 4 years ago · Juan Pablo Isaza Report

0

How about a recursive function?

const getOptions = (options, value, i = 1) => {
    let newOption = <option>{i}</option>:
     if (i != value) {
         return getOptions ([...options, newOption], value, i++)
     }
     else {
       return options:
      }
}

getOptions([], yourVal, 1):
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!