Open VaJoy opened 8 years ago
var arr = new Array(100); arr = arr.map(function(item, index) { return index; });
@mumuy 恭喜中陷阱,你可以在浏览器运行下哦,会得到一个元素全是undefined的数组 :smile:
用递归
看来我确实弱爆了
Array(100).join(",").split(",").map(function(key,index){return index;})
收集下A神的:
Array.apply(null, {length: 10}).map(Number.call, Number)
我改一下:
Object.keys(Array.apply(null, {length: 10}))
``Array.from({length:100} , function(value, index){ return index })
var arr = Array.from({length:100}, (v,k) => k);
arr
[0, 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]
Object.keys(Array(100).toString().split(","))
var list = [];
(function next(i) {
if (i < 100) {
list.push(i);
next(++i);
}
}(0));
function genArr(i, arr){
if(i < 10){
arr[i] = i++
return genArr(i, arr);
} else {
return arr;
}
}
var arr = genArr(0, [])
var a =[],b= function () { return a.length<100?(a.push(a.length)&&arguments.callee()):a }(); console.log(a,b);
[...Array(10).keys()]
Array(100).fill().map((v, i) => i)
[...Array(100).keys()]
不使用loop循环,创建一个长度为100的数组,并且每个元素的值等于它的下标
从某站看到的一道淘宝笔试题,挺有趣的,大家有兴趣可以做一做 :smile: 建议自行完成哦别搜索,不然就直接找到答案了