Skip to content

Commit 32a245c

Browse files
authored
Merge pull request #111 from Abdelrahmansherwida/master
Object.keys, values, entries
2 parents 217ff68 + b5105b7 commit 32a245c

1 file changed

Lines changed: 32 additions & 44 deletions

File tree

  • 1-js/05-data-types/09-keys-values-entries
Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,33 @@
1+
# مفاتيح الكائنات وقيمها ومدخلاتها
12

2-
# Object.keys, values, entries
3+
لنأخذ راحة صغيرة بعيدًا عن بنى البيانات ولنتحدّث عن طريقة المرور على عناصرها.
34

4-
Let's step away from the individual data structures and talk about the iterations over them.
5+
رأينا في الفصل السابق التوابِع `map.keys()`‎ و `map.values()`و `map.entries()`. هذه التوابِع عامّة وقد اتّفق معشر المطوّرين على استعمالها عند التعامل مع بنى البيانات. ولو أنشأنا بنية بيانات من الصفر بيدنا، فعلينا توفير "تنفيذ" تلك التوابِع أيضًا. هي أساسًا مدعومة لكلّ من:
56

6-
In the previous chapter we saw methods `map.keys()`, `map.values()`, `map.entries()`.
7+
- `Map` الخرائط
8+
- `Set` الأطقم
9+
- `Array` المصفوفات
710

8-
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.
11+
كما وتدعم الكائنات العادية توابِع كتلك التوابِع باختلاف بسيط في صياغتها.
912

10-
They are supported for:
13+
## التوابِع keys وvalues وentries
1114

12-
- `Map`
13-
- `Set`
14-
- `Array`
15+
هذه هي التوابِع المتاحة للتعامل مع الكائنات العادية:
1516

16-
Plain objects also support similar methods, but the syntax is a bit different.
17+
- [Object.keys(obj)](mdn:js/Object/keys) -- يُعيد مصفوفة من المفاتيح.
18+
- [Object.values(obj)](mdn:js/Object/values) -- يُعيد مصفوفة من القيم.
19+
- [Object.entries(obj)](mdn:js/Object/entries) -- يُعيد مصفوفة من أزواج [key, value].
1720

18-
## Object.keys, values, entries
21+
لاحظ رجاءً الفروق بينها وبين الخارطة مثلًا:
1922

20-
For plain objects, the following methods are available:
23+
| | Map | Object |
24+
| --------------- | ------------ | ---------------------------------------- |
25+
| صياغة الاستدعاء | `map.keys()` | `Object.keys(obj)`, لكن ليس `obj.keys()` |
26+
| قيمة الإعادة | مكرر | مصفوفة ”حقيقية“ |
2127

22-
- [Object.keys(obj)](mdn:js/Object/keys) -- returns an array of keys.
23-
- [Object.values(obj)](mdn:js/Object/values) -- returns an array of values.
24-
- [Object.entries(obj)](mdn:js/Object/entries) -- returns an array of `[key, value]` pairs.
28+
أوّل فرق واضح جليّ: علينا استدعاء Object.keys(obj)‎ لا obj.keys()‎. ولكن لماذا؟ السبب الأساس هو مرونة الاستعمال. لا تنسَ بأنّ الكائنات هي أساس كلّ بنية بيانات معقّدة في جافاسكربت. يحدث بأنّ لدينا كائن طوّرناه ليحمل بيانات data محدّدة، وفيه التابِع data.values()‎، ولكنّا نريد أيضًا استدعاء Object.values(data)‎ عليه.
2529

26-
Please note the distinctions (compared to map for example):
27-
28-
| | Map | Object |
29-
|-------------|------------------|--------------|
30-
| Call syntax | `map.keys()` | `Object.keys(obj)`, but not `obj.keys()` |
31-
| Returns | iterable | "real" Array |
32-
33-
The first difference is that we have to call `Object.keys(obj)`, and not `obj.keys()`.
34-
35-
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 `data` that implements its own `data.values()` method. And we still can call `Object.values(data)` on it.
36-
37-
The second difference is that `Object.*` methods return "real" array objects, not just an iterable. That's mainly for historical reasons.
38-
39-
For instance:
30+
الفرق الثاني هو أنّ التوابِع Object.\* تُعيد كائنات مصفوفات ”فعلية“ لا مُتعدَّدات فقط. يعزو ذلك لأسباب تاريخية بحتة. خُذ هذا المثال:
4031

4132
```js
4233
let user = {
@@ -49,7 +40,7 @@ let user = {
4940
- `Object.values(user) = ["John", 30]`
5041
- `Object.entries(user) = [ ["name","John"], ["age",30] ]`
5142

52-
Here's an example of using `Object.values` to loop over property values:
43+
وهذا مثال آخر عن كيف نستعمل Object.values للمرور على قيم الخاصيات:
5344

5445
```js run
5546
let user = {
@@ -63,24 +54,21 @@ for (let value of Object.values(user)) {
6354
}
6455
```
6556

66-
```warn header="Object.keys/values/entries ignore symbolic properties"
67-
Just like a `for..in` loop, these methods ignore properties that use `Symbol(...)` as keys.
57+
```warn header="Object.keys/values/entries تتجاهل هذه التوابِع الخاصيات الرمزية"
58+
كما تتجاهل حلقة for..in الخاصيات التي تستعمل Symbol(...)‎ مفاتيح لها، فهذه التوابِع أعلاه تتجاهلها أيضًا
6859
69-
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, there exist a method [Reflect.ownKeys(obj)](mdn:js/Reflect/ownKeys) that returns *all* keys.
60+
غالبًا يكون هذا ما نريد، ولكن لو أردت المفاتيح الرمزية أيضًا، فعليك استعمال التابِع المنفصل [Object.getOwnPropertySymbols](mdn:js/Object/getOwnPropertySymbols) إذ يُعيد مصفوفة بالمفاتيح الرمزية فقط. هناك أيضًا التابِع [Reflect.ownKeys(obj)](mdn:js/Reflect/ownKeys) إذ يُعيد المفاتيح كلها.
7061
```
7162

63+
## تعديل محتوى الكائنات
7264

73-
## Transforming objects
74-
75-
Objects lack many methods that exist for arrays, e.g. `map`, `filter` and others.
65+
ليس للكائنات تلك التوابِع المفيدة المُتاحة للعناصر (مثل map وfilter وغيرها). لو أردنا تطبيق هذه التوابِع على الكائنات فيجب أوّلًا استعمال Object.entries وبعدها Object.fromEntries:
7666

77-
If we'd like to apply them, then we can use `Object.entries` followed by `Object.fromEntries`:
67+
1. استعمل Object.entries(obj)‎ لتأخذ مصفوفة لها أزواج ”مفتاح/قيمة“ من الكائن obj.
68+
2. استعمل توابِع المصفوفات على تلك المصفوفة (مثلًا map).
69+
3. استعمل Object.fromEntries(array)‎ على المصفوفة الناتج لتُحوّلها ثانيةً إلى كائن.
7870

79-
1. Use `Object.entries(obj)` to get an array of key/value pairs from `obj`.
80-
2. Use array methods on that array, e.g. `map`.
81-
3. Use `Object.fromEntries(array)` on the resulting array to turn it back into an object.
82-
83-
For example, we have an object with prices, and would like to double them:
71+
إليك مثالًا لدينا كائنًا فيه تسعير البضائع، ونريد مضاعفتها (إذ ارتفع الدولار):
8472

8573
```js run
8674
let prices = {
@@ -91,12 +79,12 @@ let prices = {
9179

9280
*!*
9381
let doublePrices = Object.fromEntries(
94-
// convert to array, map, and then fromEntries gives back the object
82+
// ‫نحوّله إلى مصفوفة، ثمّ نستعمل الطقم، ثمّ يُعيد إلينا fromEntries الكائن المطلوب
9583
Object.entries(prices).map(([key, value]) => [key, value * 2])
9684
);
9785
*/!*
9886

9987
alert(doublePrices.meat); // 8
100-
```
88+
```
10189

102-
It may look difficult from the first sight, but becomes easy to understand after you use it once or twice. We can make powerful chains of transforms this way.
90+
ربّما تراه صعبًا أوّل وهلة، ولكن لا تقلق فسيصير أسهل أكثر متى ما بدأت استعمالها مرّة واثنتان وثلاث. يمكن أن نصنع سلسلة فعّالة من التعديلات بهذه الطريقة:

0 commit comments

Comments
 (0)