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

数组:reduce实现map功能 #19

Open
wuchaofan opened this issue Mar 19, 2018 · 0 comments
Open

数组:reduce实现map功能 #19

wuchaofan opened this issue Mar 19, 2018 · 0 comments

Comments

@wuchaofan
Copy link
Owner

wuchaofan commented Mar 19, 2018

假设一个数组arr = [2,5,7,8]
arr.map((item)=>{ return item * 2; })
如上代码会产生一个元素都为2倍的新数组。用reduce怎么实现呢,看代码:

arr.reduce((prev, next)=>{
  return prev.push(next * 2);
}, [])

重点在于reduce的第二个参数,初始化为一个空数组。

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