Underscore.JS range方法

Underscore.JS range方法

语法

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

range方法会创建一个整数列表。我们可以使用传递的参数来配置这个列表。start默认为0,指定列表的第一个元素;stop指定在增量顺序中要创建的元素,元素使用步进进行增加。默认步进为1。stop不包含在列表中。

示例

var _ = require('underscore');

//Example 1: create an array of 5 elements
result = _.range(5);
console.log(result)

//Example 2: create an array of 5 elements from 5 to 10
result = _.range(5, 11);
console.log(result)

//Example 3: create an array of elements from 0 to 20(exclusive) with step 5
result = _.range(0, 20, 5);
console.log(result)

//Example 4: create an array of 5 negative elements
result = _.range(0, -5, -1);
console.log(result)

//Example 5: create an empty array
result = _.range(0);
console.log(result)

将以上程序保存在 tester.js 中。运行以下命令以执行此程序。

命令

>node tester.js

输出

[ 0, 1, 2, 3, 4 ]
[ 5, 6, 7, 8, 9, 10 ]
[ 0, 5, 10, 15 ]
[ 0, -1, -2, -3, -4 ]
[]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程