Skip to content

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
Changes from 1 commit
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
d73ea15
Update solution.md
sunhaokk May 2, 2018
c5eff15
Update solution.md
sunhaokk May 2, 2018
9141dd0
Update task.md
sunhaokk May 2, 2018
86a2279
Update article.md
sunhaokk May 2, 2018
0a7ae48
Update solution.md
sunhaokk May 2, 2018
d0339a0
Update article.md
sunhaokk May 2, 2018
71d80e8
Update task.md
sunhaokk May 2, 2018
0a3fd35
Update task.md
sunhaokk May 2, 2018
49af46e
Update task.md
sunhaokk May 2, 2018
9ebe5df
Update task.md
sunhaokk May 2, 2018
55e6f14
Update task.md
sunhaokk May 2, 2018
cea21cc
Update solution.md
sunhaokk May 7, 2018
b4955fb
Update article.md
sunhaokk May 7, 2018
89a634a
Update article.md
sunhaokk May 7, 2018
f72fc0d
Update task.md
sunhaokk May 7, 2018
294c8b7
Update article.md
sunhaokk May 15, 2018
c208abf
Update article.md
sunhaokk May 16, 2018
f267154
Update task.md
sunhaokk May 16, 2018
e6a0c06
Update article.md
sunhaokk May 17, 2018
e2109aa
Update article.md
sunhaokk May 18, 2018
1d8fd3c
Update article.md
sunhaokk May 23, 2018
17ee2c0
Update article.md
sunhaokk May 23, 2018
d009d15
Update article.md
sunhaokk May 25, 2018
c89222d
Update task.md
sunhaokk May 25, 2018
facdf39
Update task.md
sunhaokk May 25, 2018
e039f4a
Update task.md
sunhaokk May 25, 2018
67578ae
Update task.md
sunhaokk May 25, 2018
67f6280
Update task.md
sunhaokk May 25, 2018
b5b102f
Update solution.md
sunhaokk May 25, 2018
3a1ea4f
Update task.md
sunhaokk May 25, 2018
6ac2cfb
Update task.md
sunhaokk May 25, 2018
d050382
Update task.md
sunhaokk May 25, 2018
4697dcf
Update solution.md
sunhaokk May 25, 2018
674a490
Update solution.md
sunhaokk May 25, 2018
ddace69
Update task.md
sunhaokk May 25, 2018
58540e0
Update task.md
sunhaokk May 25, 2018
8ca51a0
Update solution.md
sunhaokk May 25, 2018
62744ce
Update solution.md
sunhaokk May 25, 2018
5beeb90
Update solution.md
sunhaokk Jun 4, 2018
81c6fee
Update article.md
sunhaokk Jun 5, 2018
413edef
Update task.md
sunhaokk Jun 5, 2018
11d5066
Update task.md
sunhaokk Jun 5, 2018
b595372
Update solution.md
sunhaokk Jun 5, 2018
d94f72a
Update task.md
sunhaokk Jun 5, 2018
b13bd20
Update article.md
sunhaokk Jun 5, 2018
985a47a
Merge branch 'zh-hans' into 1-js/05-data-types/05-array-methods
sunhaokk Jun 5, 2018
37186db
Update article.md
sunhaokk Jun 6, 2018
c0c0891
Update solution.md
leviding Jun 8, 2018
cf443b7
Update solution.md
leviding Jun 9, 2018
b20a969
Update task.md
leviding Jun 9, 2018
f7c2d61
修正格式问题
leviding Jun 9, 2018
82b4710
Update article.md
sunhaokk Jun 11, 2018
6bad336
Update solution.md
sunhaokk Jun 11, 2018
cc06a0e
Update task.md
leviding Jun 12, 2018
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
Prev Previous commit
Next Next commit
Update article.md
  • Loading branch information
sunhaokk authored May 23, 2018
commit 1d8fd3c1703b49924a7f554f3fd06553bc0b2ddf
38 changes: 19 additions & 19 deletions 1-js/05-data-types/05-array-methods/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,25 +386,25 @@ alert(arr); // *!*1, 2, 15*/!*

现在它按预期工作。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

现在它按预期工作 => 现在结果符合预期了


Let's step aside and think what's happening. The `arr` can be array of anything, right? It may contain numbers or strings or html elements or whatever. We have a set of *something*. To sort it, we need an *ordering function* that knows how to compare its elements. The default is a string order.
让我们搁置一边,思考发生了什么。`arr` 可以是任何东西的数组。它可能包含数字或字符串或 html 元素或其他。我们对一组数据进行排序,我们需要一个**排序函数**,默认按字符串顺序比较。你也可以自定义如何比较它的元素。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

让我们搁置一边,思考发生了什么。 => 让我们先搁置一边,思考一下发生了什么。(有些地方的语气整体有些过于僵硬。。可以稍微通读一下。。)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arr 可以是任何东西的数组 => arr 可以是由任何东西组成的数组

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我们对一组数据进行排序,我们需要一个排序函数,默认按字符串顺序比较。你也可以自定义如何比较它的元素。=> 我们对一组数据进行排序时,需要一个排序函数来确认如何比较这些元素。默认是按字符串排序的。


The `arr.sort(fn)` method has a built-in implementation of sorting algorithm. We don't need to care how it exactly works (an optimized [quicksort](https://en.wikipedia.org/wiki/Quicksort) most of the time). It will walk the array, compare its elements using the provided function and reorder them, all we need is to provide the `fn` which does the comparison.
`arr.sort(fn)` 方法内置实现排序算法。我们不需要关心它是如何工作的(优化过的 [quicksort](https://en.wikipedia.org/wiki/Quicksort))。它将自动遍历数组,使用提供的函数比较它的元素并对它们重新排序,我们所需要的只是提供用于比较的函数 `fn`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

优化过的 [quicksort] => 大多数情况下是优化过的 [快速排序]算法


By the way, if we ever want to know which elements are compared -- nothing prevents from alerting them:
顺便说一句,如果我们想知道哪些元素进行了比较 - alert 是没有作用的:

```js run
[1, -2, 15, 2, 0, 8].sort(function(a, b) {
alert( a + " <> " + b );
});
```

The algorithm may compare an element multiple times in the process, but it tries to make as few comparisons as possible.
该算法可以在过程中多次比较元素,但它会尽可能少地进行比较。


````smart header="A comparison function may return any number"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

标题未翻译A comparison function may return any number

Actually, a comparison function is only required to return a positive number to say "greater" and a negative number to say "less".
实际上,比较函数只需要返回一个正数表示更大,而负数表示更少。

That allows to write shorter functions:
通过这个原理我们可以编写更短的函数:

```js run
let arr = [ 1, 2, 15 ];
Expand All @@ -416,20 +416,20 @@ alert(arr); // *!*1, 2, 15*/!*
````

````smart header="Arrow functions for the best"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

标题未翻译

Remember [arrow functions](info:function-expression#arrow-functions)? We can use them here for neater sorting:
[箭头函数](info:function-expression#arrow-functions)?我们也可以使用箭头函数:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

箭头函数?我们也可以使用箭头函数:=>
还记得箭头函数吗?这里使用箭头函数会更加简洁:


```js
arr.sort( (a, b) => a - b );
```

This works exactly the same as the other, longer, version above.
这与其他写法完全相同。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这与其他写法完全相同 => 这与上面的那些更长的其它写法是完全相同的

````

### reverse

The method [arr.reverse](mdn:js/Array/reverse) reverses the order of elements in `arr`.
[arr.reverse](mdn:js/Array/reverse) 方法颠倒 `arr` 中元素的顺序。

For instance:
例如:

```js run
let arr = [1, 2, 3, 4, 5];
Expand All @@ -438,15 +438,15 @@ arr.reverse();
alert( arr ); // 5,4,3,2,1
```

It also returns the array `arr` after the reversal.
它也在返回后返回数组 `arr`

### split and join
### split join

Here's the situation from the real life. We are writing a messaging app, and the person enters the comma-delimited list of receivers: `John, Pete, Mary`. But for us an array of names would be much more comfortable than a single string. How to get it?
在工作中。我们正在编写一个消息应用程序,并且该人员输入以逗号分隔的接收者列表:`JohnPeteMary`。但对我们来说,数组比单个字符串更舒适。怎么做才能获得这个数组呢?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在工作中。=> 举一个现实生活的场景的例子,


The [str.split(delim)](mdn:js/String/split) method does exactly that. It splits the string into an array by the given delimiter `delim`.
[str.split(delim)](mdn:js/String/split) 方法可以做到。它通过给定的分隔符 `delim` 将字符串分割成一个数组。

In the example below, we split by a comma followed by space:
在下面的例子中,我们用逗号分隔空格:

```js run
let names = 'Bilbo, Gandalf, Nazgul';
Expand All @@ -458,7 +458,7 @@ for (let name of arr) {
}
```

The `split` method has an optional second numeric argument -- a limit on the array length. If it is provided, then the extra elements are ignored. In practice it is rarely used though:
`split` 方法有一个可选的第二个数字参数 -- 对数组长度的限制。如果提供了,那么额外的元素将被忽略。但实际上它很少使用:

```js run
let arr = 'Bilbo, Gandalf, Nazgul, Saruman'.split(', ', 2);
Expand All @@ -467,7 +467,7 @@ alert(arr); // Bilbo, Gandalf
```

````smart header="Split into letters"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

标题未翻译

The call to `split(s)` with an empty `s` would split the string into an array of letters:
调用空的参数 `split(s)` 会将字符串分成一个字母数组:

```js run
let str = "test";
Expand All @@ -476,9 +476,9 @@ alert( str.split('') ); // t,e,s,t
```
````

The call [arr.join(str)](mdn:js/Array/join) does the reverse to `split`. It creates a string of `arr` items glued by `str` between them.
[arr.join(str)](mdn:js/Array/join) `split` 相反。它会在它们之间创建一串由 `str` 粘合的 `arr` 项。

For instance:
例如:

```js run
let arr = ['Bilbo', 'Gandalf', 'Nazgul'];
Expand Down