1

When the for loop is entered, it never stops:

 remove: function remove(e) {
     var objectToRemoveId = e.currentTarget.getAttribute('objectId').toString();
     var filteredList = this.myDto.objectList;

     for (var index = 0; index < this.myDto.objectList.length; index++) {
          var currentObject = this.myDto.objectList[index];

       if (currentObject.Id !== objectToRemoveId) {
         filteredList[filteredList.length + 1] = timeSheet;
       }
     }
  } 

Assumed that the this.myDto.ObjectList is an array with one element in it. I'm sure the problem is just staring me in the face, but I can't figure it out.

1
  • Can you try to put an alert statement with the value of index variable at the beginning of the for loop Commented Nov 17, 2010 at 16:17

4 Answers 4

9

You're adding to the same list in your loop, so every time you loop through, your this.myDto.objectList.length goes up one. It seems like you would want an empty array here:

var filteredList = this.myDto.objectList;

Like this:

var filteredList = [];

Or a copy, like this:

var filteredList = this.myDto.objectList.slice();

I'm not sure what the end result is supposed to be, adding to a list named filtered is throwing me off, but in either case, you're probably after one of the solutions above.

Sign up to request clarification or add additional context in comments.

2 Comments

@chum - I'm not sure exactly what you're after, but to clone it would be: var filteredList = this.myDto.objectList.slice();
Oops, didn't meant to delete my comment, just edit it. What I meant to do was create an array the size of my objectList and I was having a brain fart. var filteredList = []; works just as well, I keep forgetting that in javascript you don't have to declare an array length.
0

It is because you are modifying the list you are iterating over, increasing its length each iteration, thus the index is always 1 less than the length of the array.

The confusion is probably because of the reference:

var filteredList = this.myDto.objectList;

However you are still using the this.myDto.objectList reference in the loop, which is probably why you didn't spot it.

Comments

0

Can't you do something like this to make it less confusing?

 remove: function remove(e) {
     var objectToRemoveId = e.currentTarget.getAttribute('objectId').toString();
     var filteredList = new Array();

     for (var index = 0; index < this.myDto.objectList.length; index++) {
          var currentObject = this.myDto.objectList[index];

       if (currentObject.Id !== objectToRemoveId) {
         filteredList[index] = timeSheet;
       }
     }
   } 

Comments

0

There are two errors that together cause the infinite loop:

  1. filteredList is a reference to this.myDto.objectList - both variables refer to the same object. When you append increase the length of filteredList, you also increase the length of myDto.objectList. The solution to this part of the problem is use the .slice() method to copy the array.

  2. Additionally, you're using the strict comparison operator (!==) to look for your objectToRemoveId. Since the id is likely stored as a numeric value and you call .toString() on the attribute, these values can never be equal and thus you fall into the if statement every time through the loop.

Put the two together, and as you loop over your array you end up adding one term to the array on every iteration.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.