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

Day333:用尽量短的代码实现一个 array 的链式操作,将数组中的大于 10 的值进行一个累加 #1163

Open
Genzhen opened this issue Jun 30, 2021 · 4 comments
Labels
JavaScript teach_tag

Comments

@Genzhen
Copy link
Collaborator

Genzhen commented Jun 30, 2021

每日一题会在下午四点在交流群集中讨论,五点小程序中更新答案
欢迎大家在下方发表自己的优质见解

二维码加载失败可点击 小程序二维码

扫描下方二维码,收藏关注,及时获取答案以及详细解析,同时可解锁800+道前端面试题。

@Genzhen Genzhen added the JavaScript teach_tag label Jun 30, 2021
@Genzhen Genzhen changed the title Day33:用尽量短的代码实现一个 array 的链式操作,将数组中的大于 10 的值进行一个累加 Day333:用尽量短的代码实现一个 array 的链式操作,将数组中的大于 10 的值进行一个累加 Jun 30, 2021
@whenTheMorningDark
Copy link

const arr = [1, 2, 3, 4, 5, 10, 12, 13]
    console.log(arr.filter(v => v > 10).reduce((cur, next) => {
      cur += next
      return cur
    }, 0))

@wjiantao
Copy link

var arr = [1, 2, 3, 4, 5, 10, 12, 13];
arr = arr.filter((item) => item > 10).reduce((a, b) => a + b);
console.log(arr);

@huangpingcode
Copy link

huangpingcode commented Jul 16, 2021

[1, 2, 3, 4, 5, 10, 12, 13].reduce((sum, cur)=> cur > 10 ? sum : sum + cur)

@Beaulo-Lee
Copy link

Beaulo-Lee commented Dec 18, 2021

const array = [2, 10, 12, 3, 4, 20, 13, 14];
const res = array.reduce((acc, cur) => (cur > 10 ? cur + acc : acc), 0);
console.log(res);

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

No branches or pull requests

5 participants