Sfida: Iterazione delle Proprietà degli Oggetti con hasOwnProperty()
Compito
Creare un ciclo che itera sulle proprietà di un oggetto e stampa ciascuna proprietà insieme al suo valore. Tuttavia, devono essere stampate solo le proprietà che appartengono direttamente all'oggetto, escludendo quelle ereditate dalla sua catena di prototipi. Utilizzare il metodo hasOwnProperty() per ottenere questo risultato.
123456789101112const song = { name: "Bohemian Rhapsody", band: "Queen", released: "31 October 1975", duration: 355, }; ___ (___ ___ in ___) { if (song.___(key)) { console.log(`${key}:`, song[key]); } }
Output atteso:
name: Bohemian Rhapsody
band: Queen
released: 31 October 1975
duration: 355
- Utilizzo di un ciclo
for...inper iterare sulle proprietà dell'oggetto. - All'interno del ciclo, verifica che ogni proprietà sia una proprietà propria dell'oggetto utilizzando
hasOwnProperty()prima di stamparla.
123456789101112const song = { name: "Bohemian Rhapsody", band: "Queen", released: "31 October 1975", duration: 355, }; for (let key in song) { if (song.hasOwnProperty(key)) { console.log(`${key}:`, song[key]); } }
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Awesome!
Completion rate improved to 2.27
Sfida: Iterazione delle Proprietà degli Oggetti con hasOwnProperty()
Scorri per mostrare il menu
Compito
Creare un ciclo che itera sulle proprietà di un oggetto e stampa ciascuna proprietà insieme al suo valore. Tuttavia, devono essere stampate solo le proprietà che appartengono direttamente all'oggetto, escludendo quelle ereditate dalla sua catena di prototipi. Utilizzare il metodo hasOwnProperty() per ottenere questo risultato.
123456789101112const song = { name: "Bohemian Rhapsody", band: "Queen", released: "31 October 1975", duration: 355, }; ___ (___ ___ in ___) { if (song.___(key)) { console.log(`${key}:`, song[key]); } }
Output atteso:
name: Bohemian Rhapsody
band: Queen
released: 31 October 1975
duration: 355
- Utilizzo di un ciclo
for...inper iterare sulle proprietà dell'oggetto. - All'interno del ciclo, verifica che ogni proprietà sia una proprietà propria dell'oggetto utilizzando
hasOwnProperty()prima di stamparla.
123456789101112const song = { name: "Bohemian Rhapsody", band: "Queen", released: "31 October 1975", duration: 355, }; for (let key in song) { if (song.hasOwnProperty(key)) { console.log(`${key}:`, song[key]); } }
Grazie per i tuoi commenti!