Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Sfida: Iterazione delle Proprietà degli Oggetti con hasOwnProperty() | Tecniche Avanzate di Manipolazione degli Oggetti
Strutture Dati JavaScript

bookSfida: 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.

123456789101112
const song = { name: "Bohemian Rhapsody", band: "Queen", released: "31 October 1975", duration: 355, }; ___ (___ ___ in ___) { if (song.___(key)) { console.log(`${key}:`, song[key]); } }
copy

Output atteso:

name: Bohemian Rhapsody
band: Queen
released: 31 October 1975
duration: 355
  1. Utilizzo di un ciclo for...in per iterare sulle proprietà dell'oggetto.
  2. All'interno del ciclo, verifica che ogni proprietà sia una proprietà propria dell'oggetto utilizzando hasOwnProperty() prima di stamparla.
123456789101112
const 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]); } }
copy

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 4

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Awesome!

Completion rate improved to 2.27

bookSfida: 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.

123456789101112
const song = { name: "Bohemian Rhapsody", band: "Queen", released: "31 October 1975", duration: 355, }; ___ (___ ___ in ___) { if (song.___(key)) { console.log(`${key}:`, song[key]); } }
copy

Output atteso:

name: Bohemian Rhapsody
band: Queen
released: 31 October 1975
duration: 355
  1. Utilizzo di un ciclo for...in per iterare sulle proprietà dell'oggetto.
  2. All'interno del ciclo, verifica che ogni proprietà sia una proprietà propria dell'oggetto utilizzando hasOwnProperty() prima di stamparla.
123456789101112
const 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]); } }
copy

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 4
some-alt