gpt4 book ai didi

javascript - 访问 JSON 对象数组内的变量

转载 作者:行者123 更新时间:2023-11-27 23:49:37 26 4
gpt4 key购买 nike

我正在尝试访问存储在该 JSON 数组中的每个对象的“photor”和“captionr”。但它不起作用并给我一个错误“无法读取未定义的属性'photor'”

var slideshow = {

directory: "images/",

photos:[
{ "photor": "aurelius.jpg", "captionr" : "Mark Aurelius"},
{ "photor": "cesar.png", "captionr" : "Gaius Julius Ceasar"},
{ "photor": "couple.jpg", "captionr" : "Greek Couple"},
{ "photor": "flavian.jpg", "captionr" : "Flavian Woman"},
{ "photor": "lucius.jpg", "captionr" : "Lucius Verus"},
{ "photor": "lupe.jpg", "captionr" : "Emperor Caracalla"},
{ "photor": "sabina.jpg", "captionr" : "Sabina"}
],

currentPhoto: 0,

getPrevious: function(){
if (this.currentPhoto == 0)
this.currentPhoto = this.photos.length-1;
else
this.currentPhoto--;

var photo = this.directory + this.photos[this.currentPhoto][0].photor;
var caption = this.photos[this.currentPhoto][1].captionr;
return { "photo": photo, "caption": caption };
};

var photo=this.directory+this.photos[this.currentPhoto].photor;
var caption=this.photos[this.currentPhoto].captionr;

最佳答案

试试这个

var photo = this.directory + this.photos[this.当前照片].photo;

关于javascript - 访问 JSON 对象数组内的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32837653/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com