Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js]不使用loop,创建一个长度为100的数组 #19

Open
VaJoy opened this issue May 8, 2016 · 15 comments
Open

[js]不使用loop,创建一个长度为100的数组 #19

VaJoy opened this issue May 8, 2016 · 15 comments

Comments

@VaJoy
Copy link
Member

VaJoy commented May 8, 2016

不使用loop循环,创建一个长度为100的数组,并且每个元素的值等于它的下标

从某站看到的一道淘宝笔试题,挺有趣的,大家有兴趣可以做一做
😄 建议自行完成哦别搜索,不然就直接找到答案了

@mumuy
Copy link

mumuy commented May 8, 2016

var arr = new Array(100);
arr = arr.map(function(item, index) {
return index;
});

@VaJoy
Copy link
Member Author

VaJoy commented May 8, 2016

@mumuy 恭喜中陷阱,你可以在浏览器运行下哦,会得到一个元素全是undefined的数组 😄

@chengnuo
Copy link
Member

chengnuo commented May 8, 2016

用递归

@yuersmall
Copy link
Member

看来我确实弱爆了

@tianzi77
Copy link

tianzi77 commented May 9, 2016

Array(100).join(",").split(",").map(function(key,index){return index;})

@VaJoy
Copy link
Member Author

VaJoy commented May 9, 2016

收集下A神的:

Array.apply(null, {length: 10}).map(Number.call, Number) 

我改一下:

Object.keys(Array.apply(null, {length: 10}))

@Cxiaohuiyang
Copy link

``Array.from({length:100} , function(value, index){ return index })

@Keith-CY
Copy link

Keith-CY commented Jul 20, 2016

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]

@sevenCon
Copy link

Object.keys(Array(100).toString().split(","))

@anilpixel
Copy link

var list = [];

(function next(i) {
  if (i < 100) {
    list.push(i);
    next(++i);
  }
}(0));

@Yoomin233
Copy link

function genArr(i, arr){
    if(i < 10){
        arr[i] = i++
        return genArr(i, arr);
    } else {
        return arr;
    }
}
var arr = genArr(0, [])

@shaonq
Copy link

shaonq commented Dec 13, 2016

var a =[],b= function () { return a.length<100?(a.push(a.length)&&arguments.callee()):a }(); console.log(a,b);

@kylewh
Copy link

kylewh commented May 22, 2017

[...Array(10).keys()]

@zhishaofei3
Copy link

zhishaofei3 commented Feb 12, 2018

Array(100).fill().map((v, i) => i)

@liejiayong
Copy link

[...Array(100).keys()]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests