Contenido del Curso
JavaScript Data Structures
JavaScript Data Structures
1. Introduction and Prerequisites
2. Objects Fundamentals
Understanding ObjectsObject CreationNested PropertiesChallenge: Creating an ObjectAccessing Object PropertiesChallenge: Accessing Object PropertiesWorking with PropertiesChallenge: Modifying and Extending ObjectObject MethodsProperties in MethodsChallenge: Working with Object MethodsObject Fundamentals Sum Up
3. Advanced Object Manipulation
Challenge: hasOwnProperty() for Object Property Iteration
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:
- Use a
for...in
loop to iterate through the object's properties. - Within the loop, check if each property is an own property of the object using
hasOwnProperty()
before logging it.
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]); } }
¿Todo estuvo claro?
¡Gracias por tus comentarios!
Sección 3. Capítulo 4