Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Challenge: Object Property Iteration with hasOwnProperty() | Advanced Object Manipulation Techniques
JavaScript Data Structures

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

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

Expected output:

name: Bohemian Rhapsody
band: Queen
released: 31 October 1975
duration: 355
  1. Use a for...in loop to iterate through the object's properties.
  2. Within the loop, check if each property is an own property of the object using hasOwnProperty() before logging it.
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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 4

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 2.27

bookChallenge: Object Property Iteration with hasOwnProperty()

Svep för att visa menyn

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.

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

Expected output:

name: Bohemian Rhapsody
band: Queen
released: 31 October 1975
duration: 355
  1. Use a for...in loop to iterate through the object's properties.
  2. Within the loop, check if each property is an own property of the object using hasOwnProperty() before logging it.
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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 4
some-alt