gpt4 book ai didi

javascript - 如何从下一个json数组节点获取值

转载 作者:行者123 更新时间:2023-12-01 03:07:00 24 4
gpt4 key购买 nike

我有一个 JSON 问题,也许有人可以帮助我解决。我有以下 JSON 数组,我将其称为“showresults”:

    [  
   {  
      "UniqueCode":"98917",
      "NetworkUC":"c99af",
      "Name":"Born This Way",
      "ShowImage":"./assets/Born_This_Way_502x502.jpg",
      "ShowLogo":"null"
   },
   {  
      "UniqueCode":"f583f",
      "NetworkUC":"c99af",
      "Name":"Bates Motel",
      "ShowImage":"./assets/Bates_Motel_502x502.jpg",
      "ShowLogo":"null"
   },
   {  
      "UniqueCode":"66ede",
      "NetworkUC":"c99af",
      "Name":"60 Days In",
      "ShowImage":"./assets/60_Days_In_502x502.jpg",
      "ShowLogo":"null"
   },
   {  
      "UniqueCode":"c3ecf",
      "NetworkUC":"c99af",
      "Name":"Intervention",
      "ShowImage":"./assets/Intervention_502x502.jpg",
      "ShowLogo":"null"
   },
   {  
      "UniqueCode":"0cac8",
      "NetworkUC":"c99af",
      "Name":"Wahlburgers",
      "ShowImage":"./assets/Wahlburgers_502x502.jpg",
      "ShowLogo":"null"
   },
   {  
      "UniqueCode":"a5866",
      "NetworkUC":"c99af",
      "Name":"Leah Remini: Scientology and the Aftermath",
      "ShowImage":"./assets/Leah_remini_502x502.jpg",
      "ShowLogo":"null"
   },
   {  
      "UniqueCode":"263f5",
      "NetworkUC":"c99af",
      "Name":"L.A. Burning: The Riots 25 Years Later",
      "ShowImage":"./assets/LA_burning_502x502.jpg",
      "ShowLogo":"null"
   },
   {  
      "UniqueCode":"804b6",
      "NetworkUC":"c99af",
      "Name":"Biggie: The Life of Notorious B.I.G.",
      "ShowImage":"./assets/Biggie_502x502.jpg",
      "ShowLogo":"null"
   },
   {  
      "UniqueCode":"6a9ce",
      "NetworkUC":"c99af",
      "Name":"The Murder of Laci Petersen",
      "ShowImage":"./assets/Laci_paterson_502x502.jpg",
      "ShowLogo":"null"
   },
   {  
      "UniqueCode":"b5513",
      "NetworkUC":"c99af",
      "Name":"Who Killed Tupac?",
      "ShowImage":"./assets/Tupac_502x502.jpg",
      "ShowLogo":"null"
   },
   {  
      "UniqueCode":"29b95",
      "NetworkUC":"c99af",
      "Name":"Life, Animated",
      "ShowImage":"./assets/Life_animated_502x502.jpg",
      "ShowLogo":"null"
   },
   {  
      "UniqueCode":"2d576",
      "NetworkUC":"c99af",
      "Name":"Live PD",
      "ShowImage":"./assets/Livepd_502x502.jpg",
      "ShowLogo":"null"
   }
]

我创建了 2 个跨度标签,它们将显示“名称”节点值。我无法弄清楚的问题是页面当前是否显示 showresults[3] 中的内容,我希望一个 span 标记显示 showresults[2].Name 的值,另一个显示 showresults[4] 。姓名。这是我的代码:

if (showresults.length > 0){

var first = showresults[0];
var count = showresults.length;

var toparrow = document.createElement("img");
toparrow.setAttribute("class", "top_arrow");
toparrow.setAttribute("src", "./images/top_arrow.jpg");
toparrow.setAttribute("uc", UniqueCode);
toparrow.addEventListener("click",LoadVideo);

var downarrow = document.createElement("img");
downarrow.setAttribute("class", "down_arrow");
downarrow.setAttribute("src", "./images/down_arrow.jpg");
downarrow.setAttribute("uc", UniqueCode);
downarrow.addEventListener("click", LoadVideo);

var backShow = document.createElement("span");
backShow.setAttribute("class","back");
backShow.setAttribute("uc", UniqueCode);
backShow.addEventListener("click", LoadVideo);

var forwardShow = document.createElement("span");
forwardShow.setAttribute("class","forward");
forwardShow.addEventListener("click", LoadVideo);

for (var i = 0; i < showresults.length; i++) {

if (i == 0){

var curobj = showresults[i];
var curobjnext = showresults[i++];
var curobjprevious = showresults[i--];
var UniqueCode = curobj["UniqueCode"];
var Name = curobj["Name"];
var NameNext = curobjnext["Name"];
var NamePrevious = curobjprevious[Name];

backShow.innerHTML = NamePrevious;
forwardShow.innerHTML = NameNext;

if(curobj == first){
$('#video').append(downarrow);
$('#video').append(forwardShow);
} else {
$('#video').append(toparrow);
$('#video').append(downarrow);
$('#video').append(backShow);
$('#video').append(forwardShow);
}


}


}


}

“NameNext”和“NamePrevious”变量未返回预期结果。有人可以指出我正确的方向吗?

最佳答案

将其更改为:

if (showresults.length > 0){

var first = showresults[0];
var count = showresults.length;

var toparrow = document.createElement("img");
toparrow.setAttribute("class", "top_arrow");
toparrow.setAttribute("src", "./images/top_arrow.jpg");
toparrow.setAttribute("uc", UniqueCode);
toparrow.addEventListener("click",LoadVideo);

var downarrow = document.createElement("img");
downarrow.setAttribute("class", "down_arrow");
downarrow.setAttribute("src", "./images/down_arrow.jpg");
downarrow.setAttribute("uc", UniqueCode);
downarrow.addEventListener("click", LoadVideo);

var backShow = document.createElement("span");
backShow.setAttribute("class","back");
backShow.setAttribute("uc", UniqueCode);
backShow.addEventListener("click", LoadVideo);

var forwardShow = document.createElement("span");
forwardShow.setAttribute("class","forward");
forwardShow.addEventListener("click", LoadVideo);

for (var i = 0; i < showresults.length; i++) {

if (i == 0){

var curobj = showresults[i];
var curobjnext = showresults[i+1]; // change here
var curobjprevious = showresults[i-1]; // and here
var UniqueCode = curobj["UniqueCode"];
var Name = curobj["Name"];
var NameNext = curobjnext["Name"];
var NamePrevious = curobjprevious[Name];

backShow.innerHTML = NamePrevious;
forwardShow.innerHTML = NameNext;

if(curobj == first){
$('#video').append(downarrow);
$('#video').append(forwardShow);
} else {
$('#video').append(toparrow);
$('#video').append(downarrow);
$('#video').append(backShow);
$('#video').append(forwardShow);
}


}


}


}

但我不明白的是,您只通过以下方式显示第一个项目:if (i == 0){,这意味着在 showresults[i-1] i 将为 -1...因此要么通过 showresults[showresults.length] 无限,要么不显示之前的结果...

关于javascript - 如何从下一个json数组节点获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46109382/

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