Challenge: Object Property Iteration with hasOwnProperty()
Task
Create a loop that iterates through the properties of an object and prints each property along with its value. However, you should only print properties that belong directly to the object, not those inherited from its prototype chain. Utilize the hasOwnProperty()
method to achieve this.
const song = { name: "Bohemian Rhapsody", band: "Queen", released: "31 October 1975", duration: 355, }; ___ (___ ___ in ___) { if (song.___(key)) { console.log(`${key}:`, song[key]); } }
Expected output:
name: Bohemian Rhapsodyband: Queenreleased: 31 October 1975duration: 355
¡Gracias por tus comentarios!