如何正确显示我的数组?初学者里面

你好吗?

现在,我正在用 Javascript 学习数组。我已经完成了一个将数组内容显示到 HTML 文件中的函数,但我只收到“未定义”。

首先,我尝试修改“contenido”内部的代码,但我只将“字符”接收到0位置,例如:contenido += "<div id=d"+posicion+">Titulo: " +系列标题[0]。它给我返回了“D”,德克斯特。

我究竟做错了什么?

这是我的代码。

/*Creating a class to structure the information of a TV show by saving the title, theme, array with the main actors and saving the favorite actor in the array.

*/

class SerieTV {

    constructor (titulo, tematica, actoresPrincipales){

        var arrayActores = new Array();

        this.titulo=titulo;

        this.tematica=tematica;

        this.actores=actoresPrincipales;


/* Adding a function to generate a random favorite actor.

*/    

        this.generaActorFavorito = function(){

            var long = actoresPrincipales.length;

            let calc = Math.floor(Math.random()*(long));

           arrayActores = actoresPrincipales[calc];

           console.log(arrayActores);

        }

        

    }

}

/* Creating 3 series, the 1st with 2 actors, 2nd with 3 and 3rd with 4. Later, adding it to a new array called "total_series."

*/

var show01= new SerieTV('Dexter ',  'Drama ', ['Michael C Hall ' ,'Jennifer Carpenter']);

show01.generaActorFavorito();


var show02 = new SerieTV('Samurai Gourmet' ,  'Cocina' ,  ['Naoto Takenaka' ,'Tetsuji Tamayama' , 'Honami Suzuki '] );

show02.generaActorFavorito();


var show03 = new SerieTV ('Breaking Bad ', 'Drama ', ['Aaron Paul ','Bryan Cranston ', 'RJ Mitte ', 'Anna Gunn ']); 

show03.generaActorFavorito();


console.log("-------------------------");


var total_series = new Array();

total_series.push(show01);

total_series.push(show02);

total_series.push(show03);

console.log(total_series);


console.log("-------------------------");

http://img4.mukewang.com/649d9967000116db06510271.jpg


富国沪深
浏览 130回答 2
2回答

幕布斯6054654

要访问类的成员,请使用点表示法语法。contenido&nbsp;+=&nbsp;"<div&nbsp;id=d"+posicion+">Titulo:&nbsp;"+serie.titulo+"<br>&nbsp;Temática:&nbsp;"+serie.tematica+"&nbsp;<br>&nbsp;Actor&nbsp;Favorito:&nbsp;"+serie.actoresPrincipales+"&nbsp;<br>&nbsp;Actores:&nbsp;"+serie.actores+"&nbsp;<br><br>";generaActorFavorito另外,在这个语句的方法中,arrayActores = actoresPrincipales[calc]您将一个数组重新分配为一个没有意义的值,您可以简单地执行var arrayActores;而不是var arrayActores = new Array();

慕慕森

好的,问题解决了。我解决了删除方括号的问题。谢谢你们!!contenido&nbsp;+=&nbsp;"<div&nbsp;id=d"+posicion+">Titulo:&nbsp;"+serie.titulo+"<br>&nbsp;Temática:&nbsp;"+serie.tematica+"&nbsp;<br>&nbsp;Actor&nbsp;Favorito:&nbsp;"+serie.actoresPrincipales+"&nbsp;<br>&nbsp;Actores:&nbsp;"+serie.actores+"&nbsp;<br><br>";
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript