Skip to content

使用 reduce 减少一个额外声明的变量 #5

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,17 @@ Demo: https://www.etherdream.com/FunnyScript/cpu-hog/
一种统计 key 的个数,无需使用判断的写法:

```js
const map = {}
const str = 'hello world'

str.split('').forEach(key => {
map[key] = -~map[key]
})
const map = str.split('').reduce((prev, next) => {
prev[next] = -~prev[next];
return prev;
}, {});

console.log(map)
// {" ": 1, d: 1, e: 1, h: 1, l: 3, o: 2, r: 1, w: 1}
```

Demo: https://jsbin.com/lumexovida/edit?js,console,output
Demo: https://jsbin.com/cudarij/edit?js,console,output

> ~i = -(i + 1),~undefined = -1

Expand Down