Функция Underscore.js _.range()

Опубликовано: 7 Октября, 2022

Функция _.range():

  • Он используется для печати списка элементов от начала, заданного в качестве параметра, до конца, также являющегося параметром.
  • Параметры start и step являются необязательными.
  • Значение по умолчанию start равно 0, а step равно 1.
  • В сформированном списке начало включено, а конец исключено.
  • Параметр step может быть как положительным, так и отрицательным.

Синтаксис:

_.range([start], stop, [step])

Параметры:
Он принимает три аргумента:

  • Начало (по желанию)
  • Остановка
  • Шаг (необязательно)

Возвращаемое значение:
Возвращаемое значение представляет собой список от начала до конца (эксклюзивный).

Примеры:

  1. Passing only the stop parameter to the _.range() function:
    The ._range() function takes the element from the list one by one and do the specified operations on the code. Like here the operation is addition of the elements of the list. After adding all the elements, the reduce function ends. Here the starting value of memo is taken as ‘0’.




    <!-- Write HTML code here -->
    <html>
       
    <head>
        <script src
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
            console.log(_.range(7));
        </script>
    </body>
       
    </html>

    Output:

  2. Passing 2 parameters to the _.range() function:
    We can even use this function by passing only 2 parameters, i.e., the start and the stop parameters then also it will have no errors. Like hers the start parameter is 7 which will be included in the list. And the end parameter is 14 which is not included in the list as per the _.range function. So we will take the default parameter of the step parameter which is one. Hence we will get a list from 7 till 13.




    <!-- Write HTML code here -->
    <html>
       
    <head>
        <script src
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
            console.log(_.range(7, 14));
        </script>
    </body>
       
    </html>

    Output:

  3. Passing all the 3 parameters to the _.range() function:
    Here we take all the 3 parameters, i.e., the start, stop and the step of the list are mentioned. So, there is no need for the default values. Here the start is from 7 and the step is 3 that means the after 7 the element will be 7+3= 10 in the list. And the calculations will continue in the same manner till the end which is 20 comes.




    <!-- Write HTML code here -->
    <html>
       
    <head>
        <script src
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
            console.log(_.range(7, 21, 3));
        </script>
    </body>
       
    </html>

    Output:

  4. Passing the stop less than the start parameter to the _.range() function:
    Even if we pass the start parameter less than the stop parameter, the _.range() function will not give any error. It will itself adjust the step parameter as negative to reach the stop from the start given. So, the list will contain numbers from 21 till 16 as the end, 15, is not included in the list.




    <!-- Write HTML code here -->
    <html>
       
    <head>
        <script src
        </script>
    </head>
       
    <body>
        <script type="text/javascript">
            console.log(_.range(21, 15));
        </script>
    </body>
       
    </html>

    Output:

ПРИМЕЧАНИЕ:
Эти команды не будут работать в консоли Google или в Firefox, так как необходимо добавить эти дополнительные файлы, которых они не добавили.
Итак, добавьте указанные ссылки в свой HTML-файл, а затем запустите их.
Ссылки следующие:

Пример показан ниже: