Skip to content

Commit b03ca00

Browse files
authored
Merge pull request #602 from javascript-tutorial/leviding-patch-4
Update translation of 1-js/05-data-types/05-array-methods
2 parents 8c659fb + b35c176 commit b03ca00

File tree

23 files changed

+356
-273
lines changed

23 files changed

+356
-273
lines changed
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
function camelize(str) {
22
return str
3-
.split('-') // my-long-word -> ['my', 'long', 'word']
4-
.map(
3+
.split('-') // splits 'my-long-word' into array ['my', 'long', 'word']
4+
.map(
5+
// capitalizes first letters of all array items except the first one
6+
// converts ['my', 'long', 'word'] into ['my', 'Long', 'Word']
57
(word, index) => index == 0 ? word : word[0].toUpperCase() + word.slice(1)
6-
) // ['my', 'long', 'word'] -> ['my', 'Long', 'Word']
7-
.join(''); // ['my', 'Long', 'Word'] -> myLongWord
8+
)
9+
.join(''); // joins ['my', 'Long', 'Word'] into 'myLongWord'
810
}

1-js/05-data-types/05-array-methods/1-camelcase/task.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ importance: 5
44

55
# 将 border-left-width 转换成 borderLeftWidth
66

7-
写函数 `camelize(str)` 将诸如 "my-short-string" 之类的由短划线分隔的单词变成骆驼式的 "myShortString"。
7+
编写函数 `camelize(str)` 将诸如 "my-short-string" 之类的由短划线分隔的单词变成骆驼式的 "myShortString"。
88

9-
即:删除所有短横线,短横线后的每一个单词变为首字母大写
9+
即:删除所有短横线,并将短横线后的每一个单词的首字母变为大写
1010

11-
例如
11+
示例
1212

1313
```js
1414
camelize("background-color") == 'backgroundColor';
1515
camelize("list-style-image") == 'listStyleImage';
1616
camelize("-webkit-transition") == 'WebkitTransition';
1717
```
1818

19-
提示:使用 `split` 将字符串拆分成数组,然后将其转换 `join` 并返回
19+
提示:使用 `split` 将字符串拆分成数组,对其进行转换之后再 `join` 回来

1-js/05-data-types/05-array-methods/10-average-age/task.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ importance: 4
22

33
---
44

5-
# 获取平均
5+
# 获取平均年龄
66

7-
编写 `getAverageAge(users)` 函数,该函数获取一个具有 age 属性的对象数组,并获取平均值
7+
编写 `getAverageAge(users)` 函数,该函数获取一个具有 `age` 属性的对象数组,并返回平均年龄
88

9-
平均的公式是 `(age1 + age2 + ... + ageN) / N`
9+
平均值的计算公式是 `(age1 + age2 + ... + ageN) / N`
1010

1111
例如:
1212

@@ -19,4 +19,3 @@ let arr = [ john, pete, mary ];
1919

2020
alert( getAverageAge(arr) ); // (25 + 30 + 29) / 3 = 28
2121
```
22-

1-js/05-data-types/05-array-methods/11-array-unique/solution.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
遍历数组
1+
让我们先遍历数字:
22
- 对于每个元素,我们将检查结果数组是否已经有该元素。
3-
- 如果有,则忽略,否则添加结果
3+
- 如果有,则忽略,否则将其添加到结果中
44

5-
```js run
5+
```js run demo
66
function unique(arr) {
77
let result = [];
88

@@ -24,16 +24,16 @@ alert( unique(strings) ); // Hare, Krishna, :-O
2424

2525
代码有效,但其中存在潜在的性能问题。
2626

27-
方法 `result.includes(str)` 在内部遍历数组 `result` 并将每个元素与 `str` 进行比较以找到匹配项。
27+
方法 `result.includes(str)` 在内部遍历数组 `result`并将每个元素与 `str` 进行比较以找到匹配项。
2828

29-
所以如果 `result` 中有 `100` 个元素,并且没有一个匹配上 `str`,那么它将遍历整个 `result` 并进行完全的 `100` 比较。如果 `result` 很大,比如 `10000`那么会有 `10000` 次的比较。
29+
所以如果 `result` 中有 `100` 个元素,并且没有任何一项与 `str` 匹配,那么它将遍历整个 `result` 并进行 `100` 次比较。如果 `result` 很大,比如 `10000`那么就会有 `10000` 次的比较。
3030

31-
这本身并不是问题,因为 JavaScript 引擎速度非常快,所以遍历 10000 次就是几微秒的事
31+
这本身并不是问题,因为 JavaScript 引擎速度非常快,所以遍历一个有 `10000` 个元素的数组只需要几微秒
3232

33-
但是我们在 `for `循环中为 `arr` 的每个元素做了这样的测试
33+
但是我们在 `for `循环中对 `arr` 的每个元素都进行了一次检测
3434

35-
所以如果 `arr.length``10000`,我们会有 `10000 * 10000` = 100 百万的比较。好多啊
35+
因此,如果 `arr.length``10000`,我们会有 `10000 * 10000` = 1 亿次的比较。那真的太多了
3636

3737
所以该解决方案仅适用于小型数组。
3838

39-
进一步, <info:map-set-weakmap-weakset> 我们将看到如何优化它
39+
进一步,在后面的 <info:map-set> 一章中,我们将看到如何对该方法进行优化

1-js/05-data-types/05-array-methods/11-array-unique/task.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ importance: 4
44

55
# 数组去重
66

7-
`arr` 是一个数组
7+
`arr` 是一个数组
88

9-
创建一个函数 `unique(arr)`返回去除重复元素的 arr。
9+
创建一个函数 `unique(arr)`返回去除重复元素后的数组 `arr`
1010

1111
例如:
1212

1-js/05-data-types/05-array-methods/12-reduce-object/task.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ importance: 4
44

55
# 从数组创建键(值)对象
66

7-
假设我们有以下形式的用户数组 `{id:..., name:..., age... }`
7+
假设我们收到了一个用户数组,形式为:`{id:..., name:..., age... }`
88

99
创建一个函数 `groupById(arr)` 从该数组创建对象,以 `id` 为键(key),数组项为值。
1010

@@ -20,7 +20,7 @@ let users = [
2020
let usersById = groupById(users);
2121

2222
/*
23-
// 调用方法后我们得到:
23+
// 调用函数后我们得到:
2424
2525
usersById = {
2626
john: {id: 'john', name: "John Smith", age: 20}
@@ -31,8 +31,8 @@ usersById = {
3131
```
3232

3333

34-
处理服务端数据时,此功能很有用
34+
处理服务端数据时,这个函数很有用
3535

36-
在这个任务里我们假设 `id` 是唯一的。没有哪两个数组项具有相同的 `id`
36+
在这个任务里我们假设 `id` 是唯一的。没有两个具有相同 `id` 的数组项
3737

38-
请使用数组 `.reduce` 方法解决
38+
请在解决方案中使用数组的 `.reduce` 方法
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
```js run demo
2+
function filterRange(arr, a, b) {
3+
// added brackets around the expression for better readability
4+
return arr.filter(item => (a <= item && item <= b));
5+
}
6+
7+
let arr = [5, 3, 8, 1];
8+
9+
let filtered = filterRange(arr, 1, 4);
10+
11+
alert( filtered ); // 3,1 (matching values)
12+
13+
alert( arr ); // 5,3,8,1 (not modified)
14+
```

1-js/05-data-types/05-array-methods/2-filter-range/task.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ importance: 4
44

55
# 过滤范围
66

7-
写一个函数 `filterRange(arr, a, b)` 获取一个数组 `arr`查找 `a``b` 之间的元素并返回它们的数组
7+
写一个函数 `filterRange(arr, a, b)`,该函数获取一个数组 `arr`在其中查找数值大小在 `a``b` 之间的元素,并返回它们的数组
88

99
该函数不应该修改原数组。它应该返回新的数组。
1010

1-js/05-data-types/05-array-methods/3-filter-range-in-place/_js.view/solution.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
32
function filterRangeInPlace(arr, a, b) {
43

54
for (let i = 0; i < arr.length; i++) {
@@ -12,4 +11,4 @@ function filterRangeInPlace(arr, a, b) {
1211
}
1312
}
1413

15-
}
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
```js run demo
2+
function filterRangeInPlace(arr, a, b) {
3+
4+
for (let i = 0; i < arr.length; i++) {
5+
let val = arr[i];
6+
7+
// remove if outside of the interval
8+
if (val < a || val > b) {
9+
arr.splice(i, 1);
10+
i--;
11+
}
12+
}
13+
14+
}
15+
16+
let arr = [5, 3, 8, 1];
17+
18+
filterRangeInPlace(arr, 1, 4); // removed the numbers except from 1 to 4
19+
20+
alert( arr ); // [3, 1]
21+
```

0 commit comments

Comments
 (0)