diff --git a/1-js/05-data-types/08-keys-values-entries/01-sum-salaries/task.md b/1-js/05-data-types/08-keys-values-entries/01-sum-salaries/task.md index 211357d03a..0989b7276d 100644 --- a/1-js/05-data-types/08-keys-values-entries/01-sum-salaries/task.md +++ b/1-js/05-data-types/08-keys-values-entries/01-sum-salaries/task.md @@ -2,15 +2,15 @@ importance: 5 --- -# Sum the properties +# 属性求和 Sum the properties -There is a `salaries` object with arbitrary number of salaries. +有一个 salaries 对象,包含了任意数量的薪水。 -Write the function `sumSalaries(salaries)` that returns the sum of all salaries using `Object.values` and the `for..of` loop. +使用 Object.values 和 for..of 循环语句写一个可以返回所有薪水的和的函数 sumSalaries(salaries)。 -If `salaries` is empty, then the result must be `0`. +如果 `salaries` 是空对象,那么结果必须是 `0`。 -For instance: +举个例子: ```js let salaries = { diff --git a/1-js/05-data-types/08-keys-values-entries/02-count-properties/task.md b/1-js/05-data-types/08-keys-values-entries/02-count-properties/task.md index d7aebb1faa..9d0bc9b3f2 100644 --- a/1-js/05-data-types/08-keys-values-entries/02-count-properties/task.md +++ b/1-js/05-data-types/08-keys-values-entries/02-count-properties/task.md @@ -2,9 +2,9 @@ importance: 5 --- -# Count properties +# 计算属性数量 -Write a function `count(obj)` that returns the number of properties in the object: +写一个可以返回对象的属性数量的函数 count(obj) : ```js let user = { @@ -15,7 +15,7 @@ let user = { alert( count(user) ); // 2 ``` -Try to make the code as short as possible. +试着让代码尽可能简短。 -P.S. Ignore symbolic properties, count only "regular" ones. +提示:忽略 Symbol 属性,只计算「常规」属性。 diff --git a/1-js/05-data-types/08-keys-values-entries/article.md b/1-js/05-data-types/08-keys-values-entries/article.md index 50ba91cef5..ab050ebb35 100644 --- a/1-js/05-data-types/08-keys-values-entries/article.md +++ b/1-js/05-data-types/08-keys-values-entries/article.md @@ -1,42 +1,42 @@ -# Object.keys, values, entries +# 对象的键、值、项 -Let's step away from the individual data structures and talk about the iterations over them. +单个数据结构告一段落,下面我们让讨论如何迭代它们。 -In the previous chapter we saw methods `map.keys()`, `map.values()`, `map.entries()`. +在前面的章节中,我们认识了 `map.keys()`,`map.value()`,`map.entries()`。 -These methods are generic, there is a common agreement to use them for data structures. If we ever create a data structure of our own, we should implement them too. +这些方法是通用的,有一个共同的约定来将它们用于各种数据结构。如果我们创建一个我们自己的数据结构,我们也应该实现这些方法。 -They are supported for: +它们支持: - `Map` - `Set` -- `Array` (except `arr.values()`) +- `Array`(除了 `arr.values()`) -Plain objects also support similar methods, but the syntax is a bit different. +纯对象也支持类似的方法,但是语法上有一些不同 -## Object.keys, values, entries +## Object.keys、values、entries 三个方法 -For plain objects, the following methods are available: +对于纯对象,下列方法是可用的: -- [Object.keys(obj)](mdn:js/Object/keys) -- returns an array of keys. -- [Object.values(obj)](mdn:js/Object/values) -- returns an array of values. -- [Object.entries(obj)](mdn:js/Object/entries) -- returns an array of `[key, value]` pairs. +- [Object.keys(obj)](mdn:js/Object/keys) —— 返回一个包含该对象全部的键的数组。 +- [Object.values(obj)](mdn:js/Object/values) —— 返回一个包含该对象全部的值的数组。 +- [Object.entries(obj)](mdn:js/Object/entries) —— 返回一个包含该对象全部 [key, value] 键值对的数组。 -...But please note the distinctions (compared to map for example): +... 但是请注意区别(比如说跟 map 的区别): | | Map | Object | |-------------|------------------|--------------| -| Call syntax | `map.keys()` | `Object.keys(obj)`, but not `obj.keys()` | -| Returns | iterable | "real" Array | +| 调用语法 | `map.keys()` | `Object.keys(obj)`,而不是 `obj.keys()` | +| 返回值 | 可迭代项 |「真正的」数组 -The first difference is that we have to call `Object.keys(obj)`, and not `obj.keys()`. +第一个区别是在对象中我们的调用语法是 `Object.keys(obj)`,而不是 `obj.keys()`。 -Why so? The main reason is flexibility. Remember, objects are a base of all complex structures in JavaScript. So we may have an object of our own like `order` that implements its own `order.values()` method. And we still can call `Object.values(order)` on it. +为什么会这样?主要原因是保持灵活。请记住,在 JavaScript 中对象是所有复杂数据结构的基础。所以我们可能有一个我们自己创建的对象,比如 `order`,它实现了自己的方法 `order.values()`。同时,我们依然可以对它调用 `Object.values(order)` 方法。 -The second difference is that `Object.*` methods return "real" array objects, not just an iterable. That's mainly for historical reasons. +第二个区别是 `Object.*` 方法返回的是「真正的」数组对象,而不是可迭代项。这主要是历史原因。 -For instance: +举个例子: ```js let user = { @@ -49,7 +49,7 @@ let user = { - `Object.values(user) = ["John", 30]` - `Object.entries(user) = [ ["name","John"], ["age",30] ]` -Here's an example of using `Object.values` to loop over property values: +这里有一个使用 `Object.valuesa` 来遍历属性值的例子: ```js run let user = { @@ -57,14 +57,15 @@ let user = { age: 30 }; -// loop over values +// 遍历所有的值 for (let value of Object.values(user)) { alert(value); // John, then 30 } ``` -## Object.keys/values/entries ignore symbolic properties -Just like a `for..in` loop, these methods ignore properties that use `Symbol(...)` as keys. +## Object.keys/values/entries 忽略 Symbol 类型的属性 -Usually that's convenient. But if we want symbolic keys too, then there's a separate method [Object.getOwnPropertySymbols](mdn:js/Object/getOwnPropertySymbols) that returns an array of only symbolic keys. Also, the method [Reflect.ownKeys(obj)](mdn:js/Reflect/ownKeys) returns *all* keys. +就像 `for..in` 循环,这些方法会忽略使用 `Symbol(...)` 作为键的属性。 + +通常这很方便。但是如果我们也想要获得 Symbol 类型的键,那么有另外不同的方法 [Object.getOwnPropertySymbols](mdn:js/Object/getOwnPropertySymbols), 它会返回一个只包含 Symbol 类型的键的数组。同样,[Reflect.ownKeys(obj)](mdn:js/Reflect/ownKeys) 方法会返回「所有」键。