无法读取属性,变量undefined-Javascript

问题描述:

我想绘制一个书架使用书籍和嵌套循环的书架。 for循环为本书绘制书架,书籍和标题以及明星(评级)。但是,在尝试访问星星变量时,我收到了“明星未定义错误”。任何帮助深表感谢。无法读取属性,变量undefined-Javascript

var appsPerShelf = 4; 

var apps = [ 
{title: "My House", author:"Claire", stars: 4}, 
{title: "My Animal":, author: "Claire", stars: 5}, 
{title: "Funny Face", author: "Claire", stars: 2}, 
{title: "My Inital", author: "Claire", stars:3}, 
{title: "Dancing Animals", author:"Claire", stars: 1}, 
{title: "Racing Animals", author:"Claire", stars: 5}, 
{title: "Resize Animal", author: "Claire", stars: 3}, 
{title: "App Bookshelf", author: "Claire",stars: 2}, 
{title: "Ball Follow App", author:"Claire", stars: 3}, 
{title: "Red Soz Quiz App", author:"Claire", stars: 5}, 
{title: "Zoo App", author:"Claire", stars: 4}, 
{title: "Dry Animal App", author:"Claire", stars: 5}, 
{title: "Dancing Animals Fun", author:"Claire", stars: 3}, 
{title: "Racing Animals Fun", author:"Claire", stars: 1}, 
{title: "Red Sox Quiz", author: "Claire", stars: 3} 
]; 




//shelf drawer 
for(var i=0; i<14%appsPerShelf; i++){ 
fill(173, 117, 33); 
rect(0, 20+(20*i), width, 10); 

//app drawer 
for(var j=0; j<appsPerShelf; j++){ 
    fill(214, 255, 219); 
    rect(10, 20, 90, 100); 
    fill(0, 0, 0); 
    text(apps[j].title, 15+(20*i), 19+(20*i), 70, 100); 

//star drawer 
if(apps[i*appsPerShelf+j].stars){ 
for(var k=0; apps[i*appsPerShelf+j].stars; k++){ 
    var img = getImage("cute/Star"); 
    }} 
} 
} 
+0

你的意思是'不能读取undefined'的财产 '明星'? – Marty

+0

是的!我不会如果这是一个问题的任何不同 –

+0

您的问题表明'星星未定义的错误',而实际的问题是,你试图访问'星星'的东西是'undefined'('apps [i * appsPersShelf + j]'解析为'undefined',所以没有任何属性)。 – Marty

你行更改此

for (var k = 0; k < apps[i * appsPerShelf + j].stars; k++) 

例如

var appsPerShelf = 4; 
 

 
var apps = [{ 
 
    title: "My House", 
 
    author: "Claire", 
 
    stars: 4 
 
    }, 
 
    { 
 
    title: "My Animal", 
 
    author: "Claire", 
 
    stars: 5 
 
    }, 
 
    { 
 
    title: "Funny Face", 
 
    author: "Claire", 
 
    stars: 2 
 
    }, 
 
    { 
 
    title: "My Inital", 
 
    author: "Claire", 
 
    stars: 3 
 
    }, 
 
    { 
 
    title: "Dancing Animals", 
 
    author: "Claire", 
 
    stars: 1 
 
    }, 
 
    { 
 
    title: "Racing Animals", 
 
    author: "Claire", 
 
    stars: 5 
 
    }, 
 
    { 
 
    title: "Resize Animal", 
 
    author: "Claire", 
 
    stars: 3 
 
    }, 
 
    { 
 
    title: "App Bookshelf", 
 
    author: "Claire", 
 
    stars: 2 
 
    }, 
 
    { 
 
    title: "Ball Follow App", 
 
    author: "Claire", 
 
    stars: 3 
 
    }, 
 
    { 
 
    title: "Red Soz Quiz App", 
 
    author: "Claire", 
 
    stars: 5 
 
    }, 
 
    { 
 
    title: "Zoo App", 
 
    author: "Claire", 
 
    stars: 4 
 
    }, 
 
    { 
 
    title: "Dry Animal App", 
 
    author: "Claire", 
 
    stars: 5 
 
    }, 
 
    { 
 
    title: "Dancing Animals Fun", 
 
    author: "Claire", 
 
    stars: 3 
 
    }, 
 
    { 
 
    title: "Racing Animals Fun", 
 
    author: "Claire", 
 
    stars: 1 
 
    }, 
 
    { 
 
    title: "Red Sox Quiz", 
 
    author: "Claire", 
 
    stars: 3 
 
    } 
 
]; 
 

 
//shelf drawer 
 
for (var i = 0; i < 14 % appsPerShelf; i++) { 
 

 
    //app drawer 
 
    for (var j = 0; j < appsPerShelf; j++) { 
 
    console.log(apps[j].title); 
 
    //star drawer 
 
    var stars = ""; 
 
    for (var k = 0; k < apps[i * appsPerShelf + j].stars; k++) { 
 
     stars += "*"; 
 
    } 
 
    console.log(stars); 
 
    } 
 
}