-
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
Changes from 50 commits
d73ea15
c5eff15
9141dd0
86a2279
0a7ae48
d0339a0
71d80e8
0a3fd35
49af46e
9ebe5df
55e6f14
cea21cc
b4955fb
89a634a
f72fc0d
294c8b7
c208abf
f267154
e6a0c06
e2109aa
1d8fd3c
17ee2c0
d009d15
c89222d
facdf39
e039f4a
67578ae
67f6280
b5b102f
3a1ea4f
6ac2cfb
d050382
4697dcf
674a490
ddace69
58540e0
8ca51a0
62744ce
5beeb90
81c6fee
413edef
11d5066
b595372
d94f72a
b13bd20
985a47a
37186db
c0c0891
cf443b7
b20a969
f7c2d61
82b4710
6bad336
cc06a0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,18 +2,18 @@ importance: 5 | |
|
||
--- | ||
|
||
# Translate border-left-width to borderLeftWidth | ||
# 将 border-left-width 转换成 borderLeftWidth | ||
|
||
Write the function `camelize(str)` that changes dash-separated words like "my-short-string" into camel-cased "myShortString". | ||
写函数 `camelize(str)` 将诸如 "my-short-string" 之类的由短划线分隔的单词变成骆驼式的 "myShortString"。 | ||
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. 写出一个函数 |
||
|
||
That is: removes all dashes, each word after dash becomes uppercased. | ||
即:删除所有短横线,短横线后的第一个单词变为大写。 | ||
|
||
Examples: | ||
例如: | ||
|
||
```js | ||
camelize("background-color") == 'backgroundColor'; | ||
camelize("list-style-image") == 'listStyleImage'; | ||
camelize("-webkit-transition") == 'WebkitTransition'; | ||
``` | ||
|
||
P.S. Hint: use `split` to split the string into an array, transform it and `join` back. | ||
提示:使用 `split` 将字符串拆分成数组,然后将其转换 `join` 并返回。 |
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 }; | ||
|
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> 我们将看到如何优化它。 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,19 +25,19 @@ alert( usersMapped[0].id ); // 1 | |
alert( usersMapped[0].fullName ); // John Smith | ||
``` | ||
|
||
Please note that in for the arrow functions we need to use additional brackets. | ||
请注意,在箭头函数中,我们需要使用额外的括号。 | ||
|
||
We can't write like this: | ||
我们不能这样写: | ||
```js | ||
let usersMapped = users.map(user => *!*{*/!* | ||
fullName: `${user.name} ${user.surname}`, | ||
id: user.id | ||
}); | ||
``` | ||
|
||
As we remember, there are two arrow functions: without body `value => expr` and with body `value => {...}`. | ||
我们记得,有两个箭头函数写法:直接返回值`value => expr` 和带主体的 `value => {...}`。 | ||
|
||
Here JavaScript would treat `{` as the start of function body, not the start of the object. The workaround is to wrap them in the "normal" brackets: | ||
JavaScript 会把 `{` 作为函数体的开始,而不是对象的开始。解决方法是将它们包装在正常括号中: | ||
|
||
```js | ||
let usersMapped = users.map(user => *!*({*/!* | ||
|
@@ -46,6 +46,6 @@ let usersMapped = users.map(user => *!*({*/!* | |
})); | ||
``` | ||
|
||
Now fine. | ||
也就是把 `value => {...}` 缓存 `value => ({...})`。 | ||
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. 这句是怎么翻译过来的?原文就是 Now fine. |
||
|
||
|
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.
这里有两种结果 => 可能有两种结果
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.
结果 本身包含总结的意思 具有选择性。 原来意思就是二选一。 如果用可能就改变原来意思