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

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,159 Points

Iterating through and updating one property in a JavaScript Array of Objects

I am writing a function that will iterate through a JavaScript Array of Objects (reproduced below) with the goal of updating the distance property in every member of the array. My function (also reproduced below) unfortunately updates the distance property in the FIRST member of the array only. I've worked on this for hours and tweaked and tried everything and cannot figure out how to make the property update for ALL objects in the array. Starting to feel stupid, which is not helping. What am I missing? Thanks in advance... NJM

// This is the business array.
var business = [
  {
    name: 'Red Apple',
    distance: 2,
    price: 3,
    rating: 1,
    category: 'restaurant' 
  },
  {
    name: 'Zoe',
    distance: 1,
    price: 1,
    rating: 5,
    category: 'restaurant'
  },
  {
    name: 'Everything Pizza',
    distance: 6,
    price: 1,
    rating: 2,
    category: 'restaurant'
  },
  {
    name: 'Salad Place',
    distance: 2,
    price: 2,
    rating: 4,
    category: 'restaurant'
  },
  {
    name: 'Yumm',
    distance: 5,
    price: 2,
    rating: 3,
    category: 'restaurant'
  },
  {
    name: 'White Sands',
    distance: 9,
    price: 5,
    rating: 4,
    category: 'hotel'
  },
  {
    name: 'California Foods',
    distance: 8,
    price: 5,
    rating: 4,
    category: 'restaurant'
  },
  {
    name: 'Good Night',
    distance: 2,
    price: 3,
    rating: 3,
    category: 'hotel'
  }
];


//too stupid to figure out how to make all the distances update
console.table(business);
business.updateLocation = function(miles){
     for (var index = 0; index < business.length; index++){
        business[index].distance += miles;

       return business;
      }      
  };
console.table(business.updateLocation(3));

3 Answers

Hey Nancy, you are doing great nothing stupid. Only mistake I can see here that return statement you are executing. Don't do that. It's getting first value and comes out from the loop. That's why you are getting the updated value of first one. So, just omit your return statement and it will work just as you expecting.

Hi your problem is the return statement.

If you give a return statement into your code then the function/loop execution stops at once. Thats why it only updated only one array object. When it came to the return it jumped out of your for loop.

business.updateLocation = function(miles){
     for (var index = 0; index < business.length; index++){
        business[index].distance += miles;

      }
      return business;
};

Notice in the code above the return is after the for loop body.

So it updates the whole array and then returns the whole array back.

Hope this helped.

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,159 Points

Thank you both for answering so promptly. I thought I had moved/removed the return statement. Learning more than one language while maintaining a busy schedule. I guess I get a little confused about what to do with the return depending on the language.

Thanks for your help and kind words. You both rock. NJM

Always ready to help.

I find myself overwhelmed from time to time since I started my web dev job. Seems I can't know enough programming languages :)