Sambit Mishra Sambit Mishra - 3 months ago 12
Javascript Question

jquery error uncaught typeerror: cannot read property 'length' of undefined

I am going through a JavaScript tutorial. I have to show the length of an array but on calling I am getting the above error.

var bio = {
"portfolio": [
{
"name": "Sambit Mishra",
"role": "Frontend Web Developer",
"contacts": {
"mobile": "+91-8888888888",
"email": "xyz@gmail.com"
},
"skills": ["Awesome ", "Frontend Design ", "Javascript ", "Python "],
"message": "Have a great day ahead."
}
]}
if(bio["skills"].length > 0) {
$("#header").append(HTMLskillsStart);

var formattedSkill = HTMLskills.replace("%data%", bio.skills[0]);
$("#skills").append(formattedSkill);
formattedSkill = HTMLskills.replace("%data%", bio.skills[1]);
$("#skills").append(formattedSkill);
formattedSkill = HTMLskills.replace("%data%", bio.skills[2]);
$("#skills").append(formattedSkill);
formattedSkill = HTMLskills.replace("%data%", bio.skills[3]);
$("#skills").append(formattedSkill);}


What I infer here is that the length property for bio["skills"] show as undefined. Help will be acknowledged. Thank you..

Answer

Use this bio.portfolio[0]['skills'].length or bio.portfolio[0].skills.length rathen then bio["skills"]