我将如何遍历一组 JS 对象并为每个对象打印一条语句? [复制]

Posted

技术标签:

【中文标题】我将如何遍历一组 JS 对象并为每个对象打印一条语句? [复制]【英文标题】:How would I loop over a group of JS objects and print a statement for each? [duplicate] 【发布时间】: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);

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

【问题讨论】:

【参考方案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);

【讨论】:

以上是关于我将如何遍历一组 JS 对象并为每个对象打印一条语句? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

在对象中循环嵌套数组

在c#中遍历对象树

D3.js 将对象绑定到数据并为每个键附加

js如何遍历并取出对象的属性名?

Zapier 编写的代码循环遍历一组对象,每个对象提取一个值,然后平均这些值

为JSON对象中的每个数组添加一个(子)按钮元素到div,并为数组中的每个对象添加子按钮(到数组按钮)