js怎么快速创建这样一个数组?

            var fs_qsw = [{number:0},{number:1},{number:2},{number:3},{number:4},{number:5},{number:6},{number:7},{number:8},{number:9}];

现在是手写的,有没有函数生成的办法?



浮云间
浏览 858回答 1
1回答

largeQ

Array.fillArray(10).fill('naive').map((v,&nbsp;i)&nbsp;=>{&nbsp;return&nbsp;{number:&nbsp;i}});Array.fromArray.from(new&nbsp;Array(10),&nbsp;(v,i)&nbsp;=>&nbsp;{&nbsp;return&nbsp;{number:&nbsp;i}});递归(function&nbsp;wallace&nbsp;(i)&nbsp;{&nbsp;return&nbsp;(i&nbsp;<&nbsp;0)&nbsp;?&nbsp;[]&nbsp;:&nbsp;wallace(i&nbsp;-&nbsp;1).concat({number:&nbsp;i});&nbsp;})(10);尾递归(function&nbsp;mistake&nbsp;(i,&nbsp;acc)&nbsp;{&nbsp;return&nbsp;(i&nbsp;<&nbsp;10)&nbsp;?&nbsp;mistake(i&nbsp;+&nbsp;1,&nbsp;acc.concat({number:&nbsp;i}))&nbsp;:&nbsp;acc;&nbsp;})(0,&nbsp;[]);Generatorfunction*&nbsp;angry(i)&nbsp;{&nbsp;&nbsp;yield&nbsp;{number:&nbsp;i};&nbsp;&nbsp;if&nbsp;(i&nbsp;<&nbsp;10)&nbsp;{&nbsp;yield*&nbsp;angry(i&nbsp;+&nbsp;1);&nbsp;} };Array.from(angry(0));apply方式Array.apply(null,&nbsp;{&nbsp;length:&nbsp;10&nbsp;}).map((v,&nbsp;i)&nbsp;=>&nbsp;i);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript