英文:
Change the values of an item after an AJAX request
问题
我尝试添加一个 if
语句,如果 value = 1
,则将 weekday_tinyint
更改为 "Sunday"
,但当我这样做时,似乎会破坏返回的对象。
以下的代码似乎可以正常工作。请告诉我如何最好地创建一个数组,然后处理数组,以便在列出它们之前对各个项目进行更改。
谢谢。
<div class="mypanel"></div>
index = 0;
day = 0;
$.getJSON('https://arizona-na.org/bmlt/main_server/client_interface/json/?switcher=GetSearchResults&services=5', function(data) {
while (index < data.length) {
if (text === undefined) {
var text = `${data[index].weekday_tinyint}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `${data[index].start_time}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `${data[index].meeting_name}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `(` + `${data[index].formats}` + `)` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` +
`<a target="_blank" href="https://maps.google.com/maps?q=` + `${data[index].latitude}` + `,` + `${data[index].longitude}` + `">` + `${data[index].location_text}` + `&nbsp;&nbsp;` + `${data[index].location_info}` + `&nbsp;&nbsp;` + `${data[index].location_street}` + `,&nbsp;&nbsp;` + `${data[index].location_municipality}` + `</a><br>`;
} else {
var text = text + `${data[index].weekday_tinyint}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `${data[index].start_time}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `${data[index].meeting_name}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `(` + `${data[index].formats}` + `)` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` +
`<a target="_blank" href="https://maps.google.com/maps?q=` + `${data[index].latitude}` + `,` + `${data[index].longitude}` + `">` + `${data[index].location_text}` + `&nbsp;&nbsp;` + `${data[index].location_info}` + `&nbsp;&nbsp;` + `${data[index].location_street}` + `,&nbsp;&nbsp;` + `${data[index].location_municipality}` + `</a><br>`;
}
index++;
}
$(".mypanel").html(text);
});
我尝试将脚本更改为以下内容,并收到错误...
var index = 0;
var daytext = "";
$.getJSON('https://arizona-na.org/bmlt/main_server/client_interface/json/?switcher=GetSearchResults&services=5', function(data) {
while (index < data.length) {
if (${data[index].weekday_tinyint} === 1)
daytext = `Sunday`;
else daytext = `other day`;
if (text === undefined) {
var text = `$daytext` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `${data[index].start_time}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `${data[index].meeting_name}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `(` + `${data[index].formats}` + `)` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` +
`<a target="_blank" href="https://maps.google.com/maps?q=` + `${data[index].latitude}` + `,` + `${data[index].longitude}` + `">` + `${data[index].location_text}` + `&nbsp;&nbsp;` + `${data[index].location_info}` + `&nbsp;&nbsp;` + `${data[index].location_street}` + `,&nbsp;&nbsp;` + `${data[index].location_municipality}` + `</a><br>`;
} else {
var text = text + `${data[index].weekday_tinyint}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `${data[index].start_time}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `${data[index].meeting_name}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `(` + `${data[index].formats}` + `)` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` +
`<a target="_blank" href="https://maps.google.com/maps?q=` + `${data[index].latitude}` + `,` + `${data[index].longitude}` + `">` + `${data[index].location_text}` + `&nbsp;&nbsp;` + `${data[index].location_info}` + `&nbsp;&nbsp;` + `${data[index].location_street}` + `,&nbsp;&nbsp;` + `${data[index].location_municipality}` + `</a><br>`;
}
index++;
}
$(".mypanel").html(text);
});
英文:
I have tried to add an if
statement to change the weekday_tinyint
to "Sunday"
if value = 1
, but when I do this it seems to mess up the returned object.
The following code seems to work just fine. Please advise me on the best way to create an array and then process the array so I can make changes to the individual items before listing them.
Thank you.
<!-- language: lang-html -->
<div class="mypanel"></div>
<!-- language: lang-js -->
index = 0;
day = 0;
$.getJSON('https://arizona-na.org/bmlt/main_server/client_interface/json/?switcher=GetSearchResults&services=5', function(data) {
while (index < data.length) {
if (text === undefined) {
var text = `${data[index].weekday_tinyint}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `${data[index].start_time}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `${data[index].meeting_name}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `(` + `${data[index].formats}` + `)` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` +
`<a target="_blank" href="https://maps.google.com/maps?q=` + `${data[index].latitude}` + `,` + `${data[index].longitude}` + `">` + `${data[index].location_text}` + `&nbsp;&nbsp;` + `${data[index].location_info}` + `&nbsp;&nbsp;` + `${data[index].location_street}` + `,&nbsp;&nbsp;` + `${data[index].location_municipality}` + `</a><br>`;
} else {
var text = text + `${data[index].weekday_tinyint}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `${data[index].start_time}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `${data[index].meeting_name}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `(` + `${data[index].formats}` + `)` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` +
`<a target="_blank" href="https://maps.google.com/maps?q=` + `${data[index].latitude}` + `,` + `${data[index].longitude}` + `">` + `${data[index].location_text}` + `&nbsp;&nbsp;` + `${data[index].location_info}` + `&nbsp;&nbsp;` + `${data[index].location_street}` + `,&nbsp;&nbsp;` + `${data[index].location_municipality}` + `</a><br>`;
}
index++;
}
$(".mypanel").html(text);
});
I tried changing the script to the following and received errors...
var index = 0;
var daytext = "";
$.getJSON('https://arizona-na.org/bmlt/main_server/client_interface/json/?switcher=GetSearchResults&services=5', function(data) {
while (index < data.length) {
if (${data[index].weekday_tinyint} === 1)
daytext = `Sunday`;
else daytext = `other day`;
if (text === undefined) {
var text = `$daytext` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `${data[index].start_time}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `${data[index].meeting_name}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `(` + `${data[index].formats}` + `)` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` +
`<a target="_blank" href="https://maps.google.com/maps?q=` + `${data[index].latitude}` + `,` + `${data[index].longitude}` + `">` + `${data[index].location_text}` + `&nbsp;&nbsp;` + `${data[index].location_info}` + `&nbsp;&nbsp;` + `${data[index].location_street}` + `,&nbsp;&nbsp;` + `${data[index].location_municipality}` + `</a><br>`;
} else {
var text = text + `${data[index].weekday_tinyint}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `${data[index].start_time}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `${data[index].meeting_name}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` + `(` + `${data[index].formats}` + `)` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` +
`<a target="_blank" href="https://maps.google.com/maps?q=` + `${data[index].latitude}` + `,` + `${data[index].longitude}` + `">` + `${data[index].location_text}` + `&nbsp;&nbsp;` + `${data[index].location_info}` + `&nbsp;&nbsp;` + `${data[index].location_street}` + `,&nbsp;&nbsp;` + `${data[index].location_municipality}` + `</a><br>`;
}
index++;
}
$(".mypanel").html(text);
});
答案1
得分: 1
你正在做的事情看起来有点复杂。我建议简化整个过程,使用.map()
函数而不是你的while循环,同时建议给你的HTML元素一个ID而不是类:
<div id="mypanel"></div>
然后你的脚本会是这样的:
$.getJSON('https://arizona-na.org/bmlt/main_server/client_interface/json/?switcher=GetSearchResults&services=5', function(data) {
var text = data.map(obj => {
var strDay = "";
switch (parseInt(obj.weekday_tinyint)) {
case 1: strDay = "Sunday"; break;
case 2: strDay = "Monday"; break;
case 3: strDay = "Tuesday"; break;
case 4: strDay = "Wednesday"; break;
case 5: strDay = "Thursday"; break;
case 6: strDay = "Friday"; break;
case 7: strDay = "Saturday"; break;
}
return `${strDay}` + ` ` +
`${obj.start_time}` + ` ` +
`${obj.meeting_name}` + ` ` +
`(` + `${obj.formats}` + `)` +
` ` +
`<a target="_blank" href="https://maps.google.com/maps?q=` + `${obj.latitude}` +
`,` + `${obj.longitude}` + `">` + `${obj.location_text}` + ` ` +
`${obj.location_info}` + ` ` + `${obj.location_street}` +
`, ` + `${obj.location_municipality}` + `</a><br>`;
});
$("#mypanel").html(text);
});
这是JavaScript map()
函数的文档:https://www.w3schools.com/jsref/jsref_map.asp
这里是可运行的 JsFiddle:JsFiddle
英文:
It all looks a bit complex what you're doing there. I would suggest to simplify the whole thing and use .map()
function instead of your while loop, also I suggest to give your html element an ID instead of the class:
<div id="mypanel"></div>
then your script would be as follows
$.getJSON('https://arizona-na.org/bmlt/main_server/client_interface/json/?switcher=GetSearchResults&services=5', function(data) {
var text = data.map(obj => {
var strDay = "";
switch (parseInt(obj.weekday_tinyint)){
case 1: strDay = "Sunday"; break;
case 2: strDay = "Monday"; break;
case 3: strDay = "Tuesday"; break;
case 4: strDay = "Wednesday"; break;
case 5: strDay = "Thursday"; break;
case 6: strDay = "Friday"; break;
case 7: strDay = "Saturday"; break;
}
return `${strDay}` +`&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` +
`${obj.start_time}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` +
`${data[index].meeting_name}` + `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` +
`(` + `${obj.formats}` + `)` +
`&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;` +
`<a target="_blank" href="https://maps.google.com/maps?q=` + `${obj.latitude}` +
`,` + `${obj.longitude}` + `">` + `${obj.location_text}` + `&nbsp;&nbsp;` +
`${obj.location_info}` + `&nbsp;&nbsp;` + `${obj.location_street}` +
`,&nbsp;&nbsp;` + `${obj.location_municipality}` + `</a><br>`;
});
$("#mypanel").html(text);
});
Here is the documentation for the JavaScript map()
function
https://www.w3schools.com/jsref/jsref_map.asp
And here is the functioning JsFiddle
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论