在 Javascript/jQuery 中创建包含两个数字之间的所有整数的数组 [英] Create array of all integers between two numbers, inclusive, in Javascript/jQuery

查看:54
本文介绍了在 Javascript/jQuery 中创建包含两个数字之间的所有整数的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下复选框:

<input type="checkbox" value="1-25" />

为了获得定义我正在寻找的范围边界的两个数字,我使用了以下 jQuery:

To get the two numbers that define the boundaries of range I'm looking for, I use the following jQuery:

var value = $(this).val();
var lowEnd = Number(value.split('-')[0]);
var highEnd = Number(value.split('-')[1]);

然后我如何创建一个包含 lowEndhighEnd 之间的所有整数的数组,包括 lowEndhighEnd> 自己?对于这个特定的例子,显然,结果数组将是:

How do I then create an array that contains all integers between lowEnd and highEnd, including lowEnd and highEnd themselves? For this specific example, obviously, the resulting array would be:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]

推荐答案

var list = [];
for (var i = lowEnd; i <= highEnd; i++) {
    list.push(i);
}

这篇关于在 Javascript/jQuery 中创建包含两个数字之间的所有整数的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆