Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3
Object.keys, values, entries#159
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
10 changes: 5 additions & 5 deletions1-js/05-data-types/09-keys-values-entries/01-sum-salaries/_js.view/solution.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
functionsečtiProdeje(prodeje) { | ||
letsoučet = 0; | ||
for (letprodej of Object.values(prodeje)) { | ||
součet +=prodej; | ||
} | ||
returnsoučet; | ||
} | ||
18 changes: 9 additions & 9 deletions1-js/05-data-types/09-keys-values-entries/01-sum-salaries/_js.view/test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
describe("sečtiProdeje", function() { | ||
it("vrátí součet prodejů", function() { | ||
letprodeje = { | ||
"Jan": 100, | ||
"Petr": 300, | ||
"Marie": 250 | ||
}; | ||
assert.equal(sečtiProdeje(prodeje), 650 ); | ||
}); | ||
it("pro prázdný objekt vrátí 0", function() { | ||
assert.strictEqual(sečtiProdeje({}), 0); | ||
}); | ||
}); |
32 changes: 16 additions & 16 deletions1-js/05-data-types/09-keys-values-entries/01-sum-salaries/solution.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
```js run demo | ||
functionsečtiProdeje(prodeje) { | ||
letsoučet = 0; | ||
for (letprodej of Object.values(prodeje)) { | ||
součet +=prodej; | ||
} | ||
returnsoučet; // 650 | ||
} | ||
letprodeje = { | ||
"Jan": 100, | ||
"Petr": 300, | ||
"Marie": 250 | ||
}; | ||
alert(sečtiProdeje(prodeje) ); // 650 | ||
``` | ||
Nebo volitelně můžeme také získat součet použitím`Object.values`a `reduce`: | ||
```js | ||
// reducecykluje nad polem prodejů, | ||
//sečte je | ||
//a vrátí výsledek | ||
functionsečtiProdeje(prodeje) { | ||
return Object.values(prodeje).reduce((a, b) => a + b, 0) // 650 | ||
} | ||
``` |
20 changes: 10 additions & 10 deletions1-js/05-data-types/09-keys-values-entries/01-sum-salaries/task.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion1-js/05-data-types/09-keys-values-entries/02-count-properties/_js.view/solution.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
functionspočítej(obj) { | ||
return Object.keys(obj).length; | ||
} | ||
14 changes: 7 additions & 7 deletions1-js/05-data-types/09-keys-values-entries/02-count-properties/_js.view/test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
describe("spočítej", function() { | ||
it("spočítá počet vlastností", function() { | ||
assert.equal(spočítej({a: 1, b: 2}), 2 ); | ||
}); | ||
it("pro prázdný objekt vrátí 0", function() { | ||
assert.equal(spočítej({}), 0 ); | ||
}); | ||
it("ignoruje symbolické vlastnosti", function() { | ||
assert.equal(spočítej({ [Symbol('id')]: 1 }), 0 ); | ||
}); | ||
}); |
16 changes: 8 additions & 8 deletions1-js/05-data-types/09-keys-values-entries/02-count-properties/task.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
112 changes: 56 additions & 56 deletions1-js/05-data-types/09-keys-values-entries/article.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,103 @@ | ||
#MetodyObject.keys, values, entries | ||
Odhlédněme nyní od individuálních datových struktur a promluvme si o iteracích nad nimi. | ||
V předchozí kapitole jsme viděli metody `mapa.keys()`, `mapa.values()`, `mapa.entries()`. | ||
Tyto metody jsou generické, existuje společná dohoda ohledně jejich používání pro datové struktury. Jestliže si sami vytvoříme vlastní datovou strukturu, měli bychom je implementovat také. | ||
Jsou podporovány v: | ||
-mapách (`Map`) | ||
-množinách (`Set`) | ||
-polích (`Array`) | ||
Podobné metody jsou podporovány i v planých objektech, ale jejich syntaxe je trochu jiná. | ||
## Object.keys, values, entries | ||
Pro plané objekty jsou k dispozici následující metody: | ||
- [Object.keys(obj)](mdn:js/Object/keys) --vrací pole klíčů. | ||
- [Object.values(obj)](mdn:js/Object/values) --vrací pole hodnot. | ||
- [Object.entries(obj)](mdn:js/Object/entries) --vrací pole dvojic `[klíč, hodnota]`. | ||
Prosíme, všimněte si rozdílů (například ve srovnání s mapou): | ||
| | Mapa | Objekt | | ||
|----------------|---------------------|-----------------------------------------| | ||
|Syntaxe volání | `mapa.keys()`| `Object.keys(obj)`,ale ne `obj.keys()` | | ||
|Vrací |iterovatelný objekt | „opravdové“ pole | | ||
Prvním rozdílem je, že musíme volat`Object.keys(obj)`,ne `obj.keys()`. | ||
Proč tomu tak je? Hlavním důvodem je flexibilita. Pamatujte, že objekty jsou základem všech složitých struktur v JavaScriptu. Můžeme tedy mít svůj vlastní objekt, např.`data`, který implementuje svou vlastní metodu`data.values()`. A přesto na něm můžeme volat`Object.values(data)`. | ||
Druhým rozdílem je, že metody `Object.*`vracejí „opravdová“ pole, ne jen iterovatelné objekty. Tak tomu je zejména z historických důvodů. | ||
Příklad: | ||
```js | ||
letuživatel = { | ||
jméno: "Jan", | ||
věk: 30 | ||
}; | ||
``` | ||
- `Object.keys(uživatel) = ["jméno", "věk"]` | ||
- `Object.values(uživatel) = ["Jan", 30]` | ||
- `Object.entries(uživatel) = [ ["jméno","Jan"], ["věk",30] ]` | ||
Zde je příklad použití`Object.values`k vytvoření cyklu nad hodnotami vlastností: | ||
```js run | ||
letuživatel = { | ||
jméno: "Jan", | ||
věk: 30 | ||
}; | ||
//cyklus nad hodnotami | ||
for (lethodnota of Object.values(uživatel)) { | ||
alert(hodnota); //Jan, poté 30 | ||
} | ||
``` | ||
```warn header="Object.keys/values/entriesignorují symbolické vlastnosti" | ||
Stejně jako cyklus `for..in`, i tyto metody ignorují vlastnosti, jejichž klíčem je`Symbol(...)`. | ||
Zpravidla nám to vyhovuje. Jestliže však chceme i symbolické klíče, existuje samostatná metoda[Object.getOwnPropertySymbols](mdn:js/Object/getOwnPropertySymbols), která vrací pole výhradně symbolických klíčů. Existuje i metoda[Reflect.ownKeys(obj)](mdn:js/Reflect/ownKeys), která vrací *všechny* klíče. | ||
``` | ||
##Transformace objektů | ||
Objekty postrádají mnohé metody, které existují pro pole, např. `map`, `filter`a jiné. | ||
Pokud je chceme použít, můžeme použít`Object.entries`, po níž bude následovat `Object.fromEntries`: | ||
1.Použijte `Object.entries(obj)`k získání pole dvojic klíč/hodnota z `obj`. | ||
2.Na tomto poli použijte metody polí, např. `map`,která tyto dvojice klíč/hodnota transformuje. | ||
3.Na výsledné pole volejte`Object.fromEntries(pole)`, která z něj opět udělá objekt. | ||
Například máme objekt s cenami a rádi bychom je zdvojnásobili: | ||
```js run | ||
letceny = { | ||
banán: 1, | ||
pomeranč: 2, | ||
maso: 4, | ||
}; | ||
*!* | ||
letdvojnásobnéCeny = Object.fromEntries( | ||
//převedeme na pole, zmapujeme každou dvojici klíč/hodnota na jinou dvojici | ||
//a pak nám funkce fromEntries znovu vytvoří objekt | ||
Object.entries(ceny).map(prvek => [prvek[0],prvek[1] * 2]) | ||
); | ||
*/!* | ||
alert(dvojnásobnéCeny.maso); // 8 | ||
``` | ||
Na první pohled to může vypadat obtížně, ale jakmiletojednou nebo dvakrát použijete, bude snadné tomu porozumět. Tímto způsobem můžeme vytvořit silné řetězce transformací. |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.