Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Looping through Properties of an Object/Changing values

Good morning Treehouse Community. I'm new to javaScript and I'm having the most difficult time trying to wrap my head around looping through properties of an Object. What I'm hoping to accomplish is changing the properties of an object as I loop through them. Any help, direction, resources would be greatly appreciated.

http://codepen.io/JohnErickson5/pen/PzKPKP

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I'm not exactly sure why you want to loop through the properties and change the values there. So, I reworked your code a bit to show you how we would loop through an object. And how we can change the values of properties directly. I hope this helps answer some of your questions and you can achieve what you're trying to do! :sparkles:

function football(wideReceiver) {
    // I now want to loop through the properties of object mikeWallace
    // I want to change the number key/value from 17 to 11
    // I want to change the team key/value from "Steelers" to Dolphins
}

var mikeWallace = {
    firstName: "Mike",
    lastName: "Wallace",
    number: 17,
    team: "Steelers"
}

mikeWallace.number = 11;
mikeWallace.team = "Dolphins";

for (var value in mikeWallace) {
  document.write("<p>  " +  mikeWallace[value] + "</p>");
}

Hope this helps!

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Steven Parker Lol as if I'm any match for your superior answers and formatting skills. Besides, you have to tell me when I'm flat out tired and wrong :wink:

Steven Parker
Steven Parker
243,656 Points

Like that happens often — NOT.   But you're faster quite frequently!   :running::dash:

Steven Parker
Steven Parker
243,656 Points
  • The individual properties can be changed directly (no loop necessary).
  • You didn't specify what looping through the properties should do, I guessed it would be display them on the page.
  • You can move or copy the loop after the value changes if you want to see the new values.
function football(wideReceiver) {
  // I now want to loop through the properties of object mikeWallace
  for (var key in wideReceiver) {
    document.write(key + ": " + wideReceiver[key] + "<br>");
  }
  // I want to change the number key/value from 17 to 11
  wideReceiver.number = 11;
  // I want to change the team key/value from "Steelers" to Dolphins
  wideReceiver.team = "Dolphins";
}

Happy coding!   -sp:sparkles: