如何在javascript中生成数字/字符序列?

有没有办法在javascript中生成字符或数字序列?

例如,我想创建包含8个1的数组。 我可以用for循环来做,但想知道是否有一个jQuery库或javascript函数可以为我做这个?

您可以根据自己的例子制作自己的可重复使用的function:

 function makeArray(count, content) { var result = []; if(typeof content == "function") { for(var i = 0; i < count; i++) { result.push(content(i)); } } else { for(var i = 0; i < count; i++) { result.push(content); } } return result; } 

然后你可以做以下任何一个:

 var myArray = makeArray(8, 1); //or something more complex, for example: var myArray = makeArray(8, function(i) { return i * 3; }); 

你可以在这里尝试一下 ,注意上面的例子根本不依赖于jQuery,所以你可以不用它。 你只是没有从图书馆获得任何东西这样的东西:)

没有for循环,这是一个解决方案:

 Array.apply(0, Array(8)).map(function() { return 1; }) 

解释如下。

Array(8)产生一个包含8个元素的稀疏数组,所有元素都是undefinedapply技巧会将其变成密集arrays。 最后,使用map ,我们将undefined的(相同)值替换为1

 for (var i=8, a=[]; i--;) a.push(1); 

使用Jquery:


 $.map($(Array(8)),function(val, i) { return i; }) 

返回:

 [0, 1, 2, 3, 4, 5, 6, 7] 

 $.map($(Array(8)),function() { return 1; }) 

返回:

 [1, 1, 1, 1, 1, 1, 1, 1] 

如果您使用较新的Javascript语法,可以使用以下方法实现:

 Array(8).fill(1) 

以下工作正常,但正如其他人指出的那样,关键字“new”是多余的。

 new Array(8).fill(1) 

序列是一个流,它在需要时计算值。 这仅需要一点内存,但在使用这些值时需要更多的CPU时间。

数组是预先计算的值列表。 这需要一些时间才能使用第一个值。 并且它需要很多内存,因为序列的所有可能值都必须存储在内存中。 你必须定义一个上限。

这意味着,在大多数情况下,创建具有序列值的数组并不是一个好主意。 相反,最好将序列实现为实际序列,仅受CPU字长的限制。

 function make_sequence (value, increment) { if (!value) value = 0; if (!increment) increment = function (value) { return value + 1; }; return function () { let current = value; value = increment (value); return current; }; } i = make_sequence() i() => 0 i() => 1 i() => 2 j = make_sequence(1, function(x) { return x * 2; }) j() => 1 j() => 2 j() => 4 j() => 8 

2016年 – 现代浏览器function已经到来。 不需要jquery。

 Array.from({length: 8}, (el, index) => 1); 

您可以使用简单的回调函数替换箭头函数,以获得更广泛的支持浏览器。 至少对我来说,这是在一步中迭代初始化数组的最简单方法。

注意:此解决方案不支持IE,但在developer.mozilla.org /中有一个polyfill 。

 The fastest way to define an array of 8 1s is to define it- var A= [1, 1, 1, 1, 1, 1, 1, 1]; // You'd have to need a lot of 1s to make a dedicated function worthwhile. // Maybe in the Matrix, when you want a lot of Smiths: Array.repeat= function(val, len){ for(var i= len, a= []; i--; ) a[i]= val; return a; } var A= Array.repeat('Smith',100) /* returned value: (String) Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith, Smith */ 

为什么不只是一个简单的连接和拆分?

 function seq(len, value) { // create an array // join it using the value we want // split it return (new Array(len + 1)).join(value).split(""); } seq(10, "a"); ["a", "a", "a", "a", "a", "a", "a", "a", "a", "a"] seq(5, 1); ["1", "1", "1", "1", "1"] 

基于Ariya Hidayat代码的Typescript方法:

 /** * Generates sequence of numbers from zero. * @ param {number} count Count of numbers in result array. * @ return {Array} Sequence of numbers from zero to (count - 1). */ public static sequence(count: number): Array { return Array.apply(0, Array(count)).map((x, i) => { return i; }); }