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

如何利用fill快速写出一个长度为5且最终结果为[1, 2, 3, 4, 5]的数组? #65

Open
lizhongzhen11 opened this issue Nov 11, 2019 · 0 comments

Comments

@lizhongzhen11
Copy link
Owner

lizhongzhen11 commented Nov 11, 2019

这个问题是前两周在某个前端群里看到的,原标题貌似并没有说用fill,但是我看到人家大概这样写的,当时就觉得非常好。直到前天晚上突然回顾发现自己没理清,遂晚上再度思考并实际编写测试才算OK。

各种简洁的写法都很厉害,说明那些人js功底很棒,加油!

// 第一种
Array(5).fill([1, 2, 3, 4, 5]).map((item, i) => item[i]) // [1, 2, 3, 4, 5]

// 第二种
Array(5).fill(1).map((item, i) => { return item + i }) // [1, 2, 3, 4, 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant