mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-03-26 17:02:27 -04:00
chore(i18n,learn): processed translations (#50292)
This commit is contained in:
@@ -8,23 +8,28 @@ dashedName: iterate-through-the-keys-of-an-object-with-a-for---in-statement
|
||||
|
||||
# --description--
|
||||
|
||||
A volte potrebbe essere necessario iterare attraverso tutte le chiavi di un oggetto. Ciò richiede una sintassi specifica in JavaScript chiamata istruzione <dfn>for...in</dfn>. Per il nostro oggetto `users`, questo potrebbe assomigliare a:
|
||||
Sometimes you need to iterate through all the keys within an object. You can use a <dfn>for...in</dfn> loop to do this. The for...in loop looks like:
|
||||
|
||||
```js
|
||||
for (let user in users) {
|
||||
console.log(user);
|
||||
```javascript
|
||||
const refrigerator = {
|
||||
'milk': 1,
|
||||
'eggs': 12,
|
||||
};
|
||||
|
||||
for (const food in refrigerator) {
|
||||
console.log(food, refrigerator[food]);
|
||||
}
|
||||
```
|
||||
|
||||
Che scriverebbe `Alan`, `Jeff` e `Sarah` sulla console - ogni valore sulla propria riga.
|
||||
This code logs `milk 1` and `eggs 12`, with each key-value pair on its own line.
|
||||
|
||||
In questa dichiarazione abbiamo definito una variabile `user`, e come puoi vedere, mentre l'iterazione prosegue attraverso l'oggetto, ad ogni ripetizione la variabile viene reimpostata a ciascuna delle chiavi, risultando nella stampa del nome di ogni utente sulla console.
|
||||
We defined the variable `food` in the loop head and this variable was set to each of the object's keys on each iteration, resulting in each food's name being printed to the console.
|
||||
|
||||
**NOTA:** Gli oggetti non mantengono un ordine sulle chiavi memorizzate come fanno gli arrays; di conseguenza la posizione di una chiave in un oggetto, o l'ordine relativo in cui appare, è irrilevante quando ci si riferisce a tale chiave o vi si accede.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Abbiamo definito una funzione `countOnline` che accetta un argomento (un oggetto users). Usa un'instruzione <dfn>for...in</dfn> all'interno di questa funzione per iterare attraverso l'oggetto users passato alla funzione e restituire il numero di utenti la cui proprietà `online` è impostata a `true`. Un esempio di oggetto users che potrebbe essere passato a `countOnline` è mostrato qui sotto. Ogni utente avrà una proprietà `online` con un valore `true` o `false`.
|
||||
We've defined a function `countOnline` which accepts one argument, `allUsers`. Use a <dfn>for...in</dfn> statement inside this function to loop through the `allUsers` object and return the number of users whose online property is set to `true`. An example of an object which could be passed to `countOnline` is shown below. Each user will have an `online` property set to either `true` or `false`.
|
||||
|
||||
```js
|
||||
{
|
||||
@@ -128,7 +133,7 @@ const users = {
|
||||
}
|
||||
}
|
||||
|
||||
function countOnline(usersObj) {
|
||||
function countOnline(allUsers) {
|
||||
// Only change code below this line
|
||||
|
||||
// Only change code above this line
|
||||
@@ -140,13 +145,13 @@ console.log(countOnline(users));
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function countOnline(usersObj) {
|
||||
let online = 0;
|
||||
for(let user in usersObj){
|
||||
if(usersObj[user].online) {
|
||||
online++;
|
||||
function countOnline(allUsers) {
|
||||
let numOnline = 0;
|
||||
for(const user in allUsers){
|
||||
if(allUsers[user].online) {
|
||||
numOnline++;
|
||||
}
|
||||
}
|
||||
return online;
|
||||
return numOnline;
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user