I've been trying this code for a for loop inplementing an array into it

const jonas = {
firstName: ‘Jonas’,
lastName: ‘Billing’,
age: 2037-1991,
job: ‘teacher’,
friends: [‘Michael’, ‘Peter’, ‘Steven’]
};

const types = [];

for (let i = 0; i < jonas.length; i++) {
    console.log(jonas[i], typeof jonas[i]);
    types.push(typeof jonas[i]);
}

console.log(types);

I expected the each property of the object to be logged into the console as i stated the condition in the for loop for the count to start at 0 and end at the maximum length of the object, and for the new array (types) to be created stating the type of element in each line of the object.
I’ve not been getting any error message on my console, so I’m not sure what I did wrong.

>Solution :

for is used to iterate for an array and you have object to interate. You can use for...in instead. Below is code snippet :

for (let key in jonas) {
  console.log(jonas[key], typeof jonas[key]);
  types.push(typeof jonas[key]);
}

console.log(types);

Leave a Reply