英文:
Breaking an array of objects
问题
只学了前端开发十周,所以请多多包涵!我试图从天气API中返回索引0到3的对象(总共4个)。我只想返回5天预报中的前4天。
我一直在阅读关于使用"break"来中断for循环的内容,我想要在i等于3时中断循环,但当我这样做时,代码不再运行。我猜想我不能在第一个if语句之后直接运行另一个if语句吗?当我加入中断语句后,屏幕上什么都不显示。
$.ajax({
url: forecast,
method: "GET",
}).then(function(response) {
console.log(response)
$('#days').empty()
var weatherArray = response.list;
for (var i = 0; i < weatherArray.length; i++) {
if (i === 3) {
break;
}
console.log(weatherArray[i]);
if (weatherArray[i].dt_txt.split(' ')[1] === '12:00:00') {
var cityMain = $('<div>');
cityMain.addClass('col-lg-2 col-md-6 mb-2 forecast-card');
var date = $("<h5>").text(response.list[i].dt_txt.split(" ")[0]);
var image = $('<img>').attr('src', 'http://openweathermap.org/img/w/' + weatherArray[i].weather[0].icon + '.png');
var minTemp = $('<p>').text('Min Temperature: ' + weatherArray[i].main.temp_min + '°C');
var maxTemp = $('<p>').text('Max Temperature : ' + weatherArray[i].main.temp_max + '°C');
cityMain.append(date).append(image).append(minTemp).append(maxTemp)
$('#days').append(cityMain);
}
}
});
注意:我修改了if (weatherArray.length === 3)
为 if (i === 3)
来在i等于3时中断循环。
英文:
Only been learning front end development for 10 weeks so please be kind! I'm trying to return objects at index 0 - 3 (out of 4) from a weather api. I only want to return the first 4 days from a 5 day forecast.
I've been reading about breaking the for loop with 'break', I had the idea to say that if i is equal to 3 then to break the loop but when I do that it doesn't run anymore of my code. I'm assuming I can't run one if statement directly after the first? When I add the break statement in, nothing displays on screen.
$.ajax({
url: forecast,
method: "GET",
}).then(function(response) {
console.log(response)
$('#days').empty()
var weatherArray = response.list;
for (var i = 0; i <weatherArray.length; i++) {
if (weatherArray.length === 3) {
break;
console.log(weatherArray[i]);
if (weatherArray[i].dt_txt.split(' ')[1] === '12:00:00') {
var cityMain = $('<div>');
cityMain.addClass('col-lg-2 col-md-6 mb-2 forecast-card>');
var date = $("<h5>").text(response.list[i].dt_txt.split(" ")[0]);
var image = $('<img>').attr('src', 'http://openweathermap.org/img/w/' + weatherArray[i].weather[0].icon + '.png');
var minTemp = $('<p>').text('Min Temperature: ' + weatherArray[i].main.temp_min + '°C');
var maxTemp = $('<p>').text('Max Temperature : ' + weatherArray[i].main.temp_max + '°C');
cityMain.append(date).append(image).append(minTemp).append(maxTemp)
$('#days').append(cityMain);
答案1
得分: 0
有几个选项。
你可以修改for
循环,只查看前0-3个元素:
for (var i = 0; i < 4; i++) {
如果你想使用break
语句,要小心如何使用它。按照当前写法,你正在查看weatherArray
数组的原始长度,这个长度永远不会改变。你可以修改它,使其查看当前数组索引。
if (i === 3) {
break;
然而,在循环中最好使用大于和小于的条件,以确保捕捉所有超出范围的条件。
if (i >= 3) {
break;
最后,你缺少一个在break
语句后面的闭合花括号,这使得你的代码只在weatherData
数组的长度为3时执行。
英文:
There are several options.
You can modify the for loop to only look at the first 0-3 elements:
for (var i = 0; i < 4; i++) {
If you want to use a break
statement be careful of how you use it. As written You're looking at the length of the raw weatherArray array which never changes. You could modify it to look at the current array index.
if (i === 3) {
break;
However, it's best to use greater than and less than conditions in loops to make sure you catch all out-of-range conditions
if (i >= 3) {
break;
Finally, you're missing a close brace immediately after the break
statement, which is making your code execute only when the the weatherData array has length 3.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论