我正在寻找以下方法的任何替代方法,以创建包含1到N的JavaScript数组,其中N仅在运行时已知。
var foo = [];
for (var i = 1; i <= N; i++) {
foo.push(i);
}
对我来说,我觉得应该有一种不用循环的方法。
我正在寻找以下方法的任何替代方法,以创建包含1到N的JavaScript数组,其中N仅在运行时已知。
var foo = [];
for (var i = 1; i <= N; i++) {
foo.push(i);
}
对我来说,我觉得应该有一种不用循环的方法。
当前回答
您可以使用Es6中的阵列填充和映射;就像一些人在回答这个问题时建议的那样。以下是一些示例:
Example-One: Array(10).fill(0).map((e,i)=>i+1)
Result-One: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Example-Two: Array(100/10).fill(0).map((e,i)=>(i*10)+10)
Result-Two:[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
我更喜欢这样,因为我觉得这很简单。
其他回答
我在寻找一个功能性的解决方案,最终得到了:
function numbers(min, max) {
return Array(max-min+2).join().split(',').map(function(e, i) { return min+i; });
}
console.log(numbers(1, 9));
注意:join().split(',')将稀疏数组转换为连续数组。
ES6中还有另一种方法,使用Array.from,它接受两个参数,第一个是arrayLike(在本例中是具有长度属性的对象),第二个是映射函数(在本示例中,我们将项映射到其索引)
Array.from({length:10}, (v,i) => i)
它更短,可用于生成偶数等其他序列
Array.from({length:10}, (v,i) => i*2)
此外,这比大多数其他方式具有更好的性能,因为它只在阵列中循环一次。查看截图以进行一些比较
//打开开发控制台以查看结果计数=100000console.time(“来自对象”)for(设i=0;i<count;i++){range=Array.from({length:10},(v,i)=>i)}console.timeEnd(“来自对象”)console.time(“来自按键”)for(设i=0;i<count;i++){range=Array.from(Array(10).keys())}console.timeEnd(“来自按键”)console.time(“应用”)for(设i=0;i<count;i++){range=Array.apply(null,{length:10}).map(函数(元素,索引){return index;})}console.timeEnd(“应用”)
不支持在ES6解决方案中创建阵列
js不适用于100阵列
1.焊盘启动
// string arr
const arr = [...``.padStart(100, ` `)].map((item, i) => i + 1 + ``);
// (100) ["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", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100"]
// number arr
const arr = [...``.padStart(100, ` `)].map((item, i) => i + 1);
// (100) [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, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
2.类型化阵列
Uint8阵列
// number arr
const arr = new Uint8Array(100).map((item, i) => i + 1);
// Uint8Array(100) [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, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
// string arr
const arr = [...new Uint8Array(100).map((item, i) => i + 1)].map((item, i) => i + 1 + ``);
// (100) ["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", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100"]
在ES6中:
Array.from({length: 1000}, (_, i) => i).slice(1);
或者更好(没有额外的变量_,也没有额外的切片调用):
Array.from({length:1000}, Number.call, i => i + 1)
或者,如果您的列表少于256个结果,您可以使用Uint8Array来获得稍快的结果(或者,您可以根据列表的长度使用其他Uint列表,例如Uint16的最大值为65535,或Uint32的最大值4294967295等。不过,正式地说,这些类型的数组只是在ES6中添加的)。例如:
Uint8Array.from({length:10}, Number.call, i => i + 1)
ES5:
Array.apply(0, {length: 1000}).map(function(){return arguments[1]+1});
或者,在ES5中,对于map函数(类似于上面ES6中Array.from函数的第二个参数),可以使用Number.call
Array.apply(0,{length:1000}).map(Number.call,Number).slice(1)
或者,如果你在这里也反对.sslice,你可以执行上面的ES5等效操作(来自ES6),比如:
Array.apply(0,{length:1000}).map(Number.call, Function("i","return i+1"))
使用ES6
const generateArray = n => [...Array(n)].map((_, index) => index + 1);