Iterating through object properties' values

Tell us what’s happening:
Describe your issue in detail here.
I don’t understand why my collection[i].property is undefined. When I console.log the property, it is either first or last for this test case which means collection[i].property should be property[i].first and it should give the value for it but all I see is undefined. Any help would be greatly appreciated!

  **Your code so far**

function whatIsInAName(collection, source) {
const arr = [];
// Only change code below this line

for(let i = 0; i < collection.length; ++i) {
for (let property in collection[i]) {
if(collection[i].hasOwnProperty(property)) {
// Only change code above this line
return arr;

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36

Challenge: Wherefore art thou

Link to the challenge:

This is due to the way Javascript understands syntax of an Object:

const obj = {
             eating: false,
             mood: "sad",
             craving: "chips"

for (const property in obj){
   console.log(obj[property]) // prints false, "sad", finally "chips"
   console.log( // tries to find an object property named "property" in obj, which doesn't exist (undefined)
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.