【问题标题】:How would I loop over a group of JS objects and print a statement for each? [duplicate]我将如何遍历一组 JS 对象并为每个对象打印一条语句? [复制]
【发布时间】:2016-12-22 05:55:37
【问题描述】:
var teams = [
                {
                  city: 'Vancouver',
                  nickname: 'Canucks',
                  league: 'NHL'
                },
                {
                  city: 'San Jose',
                  nickname: 'Earthquakes',
                  league: 'MLS'
                },
                {
                  city: 'Sacramento',
                  nickname: 'Kings',
                  league: 'NBA'
                }
                        ]

document.write("The " + this.city + " " + this.nickname + " play in the " + this.league);

我想遍历每个并为每个打印上面的语句。我该怎么做才最好?

【问题讨论】:

    标签: javascript javascript-objects


    【解决方案1】:

    var teams = [{
                  city: 'Vancouver',
                  nickname: 'Canucks',
                  league: 'NHL'
                },
                {
                  city: 'San Jose',
                  nickname: 'Earthquakes',
                  league: 'MLS'
                },
                {
                  city: 'Sacramento',
                  nickname: 'Kings',
                  league: 'NBA'
                }];
    
    for (var i = 0; i < teams.length; i++) {
       var team = teams[i];
       document.write("The " + team.city + " " + team.nickname + " play in the " + team.league + "<br/>");
    }

    以下内容也适用于您(请记住,箭头功能并非在所有浏览器中都有效。因此可能应该使用前面的示例)..

    var teams = [{
                  city: 'Vancouver',
                  nickname: 'Canucks',
                  league: 'NHL'
                },
                {
                  city: 'San Jose',
                  nickname: 'Earthquakes',
                  league: 'MLS'
                },
                {
                  city: 'Sacramento',
                  nickname: 'Kings',
                  league: 'NBA'
                }];
    
    teams.forEach(team => {
        document.write("The " + team.city + " " + team.nickname + " play in the " + team.league + "<br/>");
    });

    【讨论】:

      【解决方案2】:

      你可以使用数组的forEach方法循环遍历数组:

      teams.forEach(function(team){
          document.write("The " + team.city + " " + team.nickname + " play in the " + team.league);
      });
      

      您还可以使用更传统的 for 循环:

      for(var i=0; i<teams.length; ++i){
          document.write("The " + teams[i].city + " " + teams[i].nickname + " play in the " + teams[i].league)
      }
      

      【讨论】:

        【解决方案3】:

        不使用this..

        teams.forEach(i => {
            document.write("The " + i.city + " " + i.nickname + " play in the " + i.league);
        });
        

        如果您必须为您的家庭作业使用this 参数,那么您需要将参数设置为当前的scope。最简单的方法是创建一个新范围并将值分配给local function scope。类似的东西。

        var teams = [
                        {
                          city: 'Vancouver',
                          nickname: 'Canucks',
                          league: 'NHL'
                        },
                        {
                          city: 'San Jose',
                          nickname: 'Earthquakes',
                          league: 'MLS'
                        },
                        {
                          city: 'Sacramento',
                          nickname: 'Kings',
                          league: 'NBA'
                        }
                                ];
                                
        var printTeam = function(team){
        	this.city = team.city;
          this.nickname = team.nickname;
          this.leage = team.leage;
         	document.write("The " + this.city + " " + this.nickname + " play in the " + this.league);
        }
                                
        teams.forEach(i => {
          printTeam(i);
        }, this);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-09-06
          • 1970-01-01
          • 2018-09-04
          • 1970-01-01
          • 1970-01-01
          • 2020-04-05
          • 2018-01-04
          • 2018-12-17
          相关资源
          最近更新 更多