12
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);

I want to loop through each and print the above statement for each. How would I best do this?

0

3 Answers 3

5

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/>");
}

The following will also work for you (keep in mind that arrow functions will not work in all browsers. So the previous example should probably be used)..

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/>");
});

0
3

You can use the forEach method of arrays to loop over the array:

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

You can also use a more traditional for loop:

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

Without the use of this..

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

If you must use the this parameter for your homework assignment then you will need to set the params to the current scope. The easiest way is to create a new scope and assign the values to the local function scope. Something like.

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);

Not the answer you're looking for? Browse other questions tagged or ask your own question.