Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

The behaviour of forEach has changed to now iterate also over skipped numeric keys. #8522

Closed
runemoennike opened this issue Aug 7, 2014 · 0 comments

Comments

@runemoennike
Copy link

As of 1.2.21 (possibly before) forEach behaves differently to what it did in 1.2.18. Given an object with numeric keys, where some numbers are skipped, it now iterates also over those missing keys, so to speak.

The following code:

var input = [], output = [];

input[1] = "a";
input[3] = "b";
input[5] = "c";

angular.forEach(input, function (v) {
   output.push(v);
})
console.log(output);

Produces an output of ["a", "b", "c"] in 1.2.18 while in 1.2.21 now gives [undefined, "a", undefined, "b", undefined, "c"].

Browser: Chrome 36
OS: Windows 8

Is this intentional? I noticed it because it breaks the angular-strap datepicker.

caitp added a commit to caitp/angular.js that referenced this issue Aug 7, 2014
…ing properties)

Array.prototype.forEach will not invoke the callback function if the properety is not present in the
object. Because of this, we have the illusion of not iterating over non-added properties in a sparse
array.

From ECMAScript:

9. Repeat while k < len
     a. Let Pk be ToString(k).
     b. Let kPresent be HasProperty(O, Pk).
     c. ReturnIfAbrupt(kPresent).
     d. If kPresent is true, then
            i. Let kValue be Get(O, Pk)
            ... (steps for invoking the function and aborting if it throws)

Closes angular#8510
Closes angular#8522
caitp added a commit to caitp/angular.js that referenced this issue Aug 7, 2014
…ing properties)

Array.prototype.forEach will not invoke the callback function if the properety is not present in the
object. Because of this, we have the illusion of not iterating over non-added properties in a sparse
array.

From ECMAScript:

9. Repeat while k < len
     a. Let Pk be ToString(k).
     b. Let kPresent be HasProperty(O, Pk).
     c. ReturnIfAbrupt(kPresent).
     d. If kPresent is true, then
            i. Let kValue be Get(O, Pk)
            ... (steps for invoking the function and aborting if it throws)

Closes angular#8510
Closes angular#8522
caitp added a commit to caitp/angular.js that referenced this issue Aug 7, 2014
…ing properties)

Array.prototype.forEach will not invoke the callback function if the properety is not present in the
object. Because of this, we have the illusion of not iterating over non-added properties in a sparse
array.

From ECMAScript:

9. Repeat while k < len
     a. Let Pk be ToString(k).
     b. Let kPresent be HasProperty(O, Pk).
     c. ReturnIfAbrupt(kPresent).
     d. If kPresent is true, then
            i. Let kValue be Get(O, Pk)
            ... (steps for invoking the function and aborting if it throws)

Closes angular#8510
Closes angular#8522
@caitp caitp closed this as completed in 3623019 Aug 20, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
1 participant