Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading