-
Notifications
You must be signed in to change notification settings - Fork 1.2k
1 js/05 data types/05 array methods #101
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
Merged
leviding
merged 54 commits into
javascript-tutorial:zh-hans
from
sunhaokk:1-js/05-data-types/05-array-methods
Jun 12, 2018
Merged
Changes from all commits
Commits
Show all changes
54 commits
Select commit
Hold shift + click to select a range
d73ea15
Update solution.md
sunhaokk c5eff15
Update solution.md
sunhaokk 9141dd0
Update task.md
sunhaokk 86a2279
Update article.md
sunhaokk 0a7ae48
Update solution.md
sunhaokk d0339a0
Update article.md
sunhaokk 71d80e8
Update task.md
sunhaokk 0a3fd35
Update task.md
sunhaokk 49af46e
Update task.md
sunhaokk 9ebe5df
Update task.md
sunhaokk 55e6f14
Update task.md
sunhaokk cea21cc
Update solution.md
sunhaokk b4955fb
Update article.md
sunhaokk 89a634a
Update article.md
sunhaokk f72fc0d
Update task.md
sunhaokk 294c8b7
Update article.md
sunhaokk c208abf
Update article.md
sunhaokk f267154
Update task.md
sunhaokk e6a0c06
Update article.md
sunhaokk e2109aa
Update article.md
sunhaokk 1d8fd3c
Update article.md
sunhaokk 17ee2c0
Update article.md
sunhaokk d009d15
Update article.md
sunhaokk c89222d
Update task.md
sunhaokk facdf39
Update task.md
sunhaokk e039f4a
Update task.md
sunhaokk 67578ae
Update task.md
sunhaokk 67f6280
Update task.md
sunhaokk b5b102f
Update solution.md
sunhaokk 3a1ea4f
Update task.md
sunhaokk 6ac2cfb
Update task.md
sunhaokk d050382
Update task.md
sunhaokk 4697dcf
Update solution.md
sunhaokk 674a490
Update solution.md
sunhaokk ddace69
Update task.md
sunhaokk 58540e0
Update task.md
sunhaokk 8ca51a0
Update solution.md
sunhaokk 62744ce
Update solution.md
sunhaokk 5beeb90
Update solution.md
sunhaokk 81c6fee
Update article.md
sunhaokk 413edef
Update task.md
sunhaokk 11d5066
Update task.md
sunhaokk b595372
Update solution.md
sunhaokk d94f72a
Update task.md
sunhaokk b13bd20
Update article.md
sunhaokk 985a47a
Merge branch 'zh-hans' into 1-js/05-data-types/05-array-methods
sunhaokk 37186db
Update article.md
sunhaokk c0c0891
Update solution.md
leviding cf443b7
Update solution.md
leviding b20a969
Update task.md
leviding f7c2d61
修正格式问题
leviding 82b4710
Update article.md
sunhaokk 6bad336
Update solution.md
sunhaokk cc06a0e
Update task.md
leviding File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,13 @@ importance: 4 | |
|
||
--- | ||
|
||
# Get average age | ||
# 获取平均 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 获取平均 => 获取平均年龄 |
||
|
||
Write the function `getAverageAge(users)` that gets an array of objects with property `age` and gets the average. | ||
编写 `getAverageAge(users)` 函数,该函数获取一个具有 age 属性的对象数组,并获取平均值。 | ||
|
||
The formula for the average is `(age1 + age2 + ... + ageN) / N`. | ||
平均的公式是 `(age1 + age2 + ... + ageN) / N`。 | ||
|
||
For instance: | ||
例如: | ||
|
||
```js no-beautify | ||
let john = { name: "John", age: 25 }; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Let's walk the array items: | ||
- For each item we'll check if the resulting array already has that item. | ||
- If it is so, then ignore, otherwise add to results. | ||
遍历数组 | ||
- 对于每个元素,我们将检查结果数组是否已经有该元素。 | ||
- 如果有,则忽略,否则添加结果。 | ||
|
||
```js run | ||
function unique(arr) { | ||
|
@@ -22,18 +22,18 @@ let strings = ["Hare", "Krishna", "Hare", "Krishna", | |
alert( unique(strings) ); // Hare, Krishna, :-O | ||
``` | ||
|
||
The code works, but there's a potential performance problem in it. | ||
代码有效,但其中存在潜在的性能问题。 | ||
|
||
The method `result.includes(str)` internally walks the array `result` and compares each element against `str` to find the match. | ||
方法 `result.includes(str)` 在内部遍历数组 `result` 并将每个元素与 `str` 进行比较以找到匹配项。 | ||
|
||
So if there are `100` elements in `result` and no one matches `str`, then it will walk the whole `result` and do exactly `100` comparisons. And if `result` is large, like `10000`, then there would be `10000` comparisons. | ||
所以如果 `result` 中有 `100` 个元素,并且没有一个匹配上 `str`,那么它将遍历整个 `result` 并进行完全的 `100` 比较。如果 `result` 很大,比如 `10000`,那么会有 `10000` 次的比较。 | ||
|
||
That's not a problem by itself, because JavaScript engines are very fast, so walk `10000` array is a matter of microseconds. | ||
这本身并不是问题,因为 JavaScript 引擎速度非常快,所以遍历 10000 次就是几微秒的事。 | ||
|
||
But we do such test for each element of `arr`, in the `for` loop. | ||
但是我们在 `for `循环中为 `arr` 的每个元素做了这样的测试。 | ||
|
||
So if `arr.length` is `10000` we'll have something like `10000*10000` = 100 millions of comparisons. That's a lot. | ||
所以如果 `arr.length` 是 `10000`,我们会有 `10000 * 10000` = 100 百万的比较。好多啊。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 百万的比较。好多啊。=> 百万次的比较。这样就太多了。 |
||
|
||
So the solution is only good for small arrays. | ||
所以该解决方案仅适用于小型数组。 | ||
|
||
Further in the chapter <info:map-set-weakmap-weakset> we'll see how to optimize it. | ||
进一步,在 <info:map-set-weakmap-weakset> 我们将看到如何优化它。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
1-js/05-data-types/05-array-methods/5-copy-sort-array/solution.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
写出一个函数
camelize(str)
,能够将诸如 "my-short-string" 之类的由短横线分隔的单词变成驼峰式的 "myShortString"。