分解对象数组

huangapple go评论66阅读模式
英文:

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: &quot;GET&quot;,
      }).then(function(response) { 
         console.log(response)
         $(&#39;#days&#39;).empty()
         var weatherArray = response.list; 
         for (var i = 0; i &lt;weatherArray.length; i++) {
           if (weatherArray.length === 3) {
            break;
                   
          console.log(weatherArray[i]);
          if (weatherArray[i].dt_txt.split(&#39; &#39;)[1] === &#39;12:00:00&#39;) {
               var cityMain = $(&#39;&lt;div&gt;&#39;);
               cityMain.addClass(&#39;col-lg-2 col-md-6 mb-2 forecast-card&gt;&#39;);
               var date = $(&quot;&lt;h5&gt;&quot;).text(response.list[i].dt_txt.split(&quot; &quot;)[0]);
               var image = $(&#39;&lt;img&gt;&#39;).attr(&#39;src&#39;, &#39;http://openweathermap.org/img/w/&#39; + weatherArray[i].weather[0].icon + &#39;.png&#39;);
               var minTemp = $(&#39;&lt;p&gt;&#39;).text(&#39;Min Temperature: &#39; + weatherArray[i].main.temp_min + &#39;&#176;C&#39;);  
               var maxTemp = $(&#39;&lt;p&gt;&#39;).text(&#39;Max Temperature : &#39; + weatherArray[i].main.temp_max + &#39;&#176;C&#39;);                               
               cityMain.append(date).append(image).append(minTemp).append(maxTemp)
               
               $(&#39;#days&#39;).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 &lt; 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 &gt;= 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.

huangapple
  • 本文由 发表于 2023年2月7日 01:54:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定