admin管理员组文章数量:1022793
Is the order of iterating through an array using one of the native methods (map, forEach, reduce, filter, etc) deterministic and guaranteed by the standard?
EG, are foo, bar, baz, and qux guaranteed to be [0, 2, 6, 12]
?
const a = [1, 2, 3, 4];
const foo = a.map((item, index) => item * index);
const bar = []; a.forEach((item, index) => bar[index] = item * index);
const baz = []; a.reduce((total, item, index) => baz[index] = item * index, 0);
const qux = []; a.filter((item, index) => qux[index] = item * index);
// etc
(These are (very) contrived examples)
Is the order of iterating through an array using one of the native methods (map, forEach, reduce, filter, etc) deterministic and guaranteed by the standard?
EG, are foo, bar, baz, and qux guaranteed to be [0, 2, 6, 12]
?
const a = [1, 2, 3, 4];
const foo = a.map((item, index) => item * index);
const bar = []; a.forEach((item, index) => bar[index] = item * index);
const baz = []; a.reduce((total, item, index) => baz[index] = item * index, 0);
const qux = []; a.filter((item, index) => qux[index] = item * index);
// etc
(These are (very) contrived examples)
Share Improve this question edited Feb 22, 2018 at 0:22 matisetorm 8538 silver badges21 bronze badges asked Aug 15, 2017 at 19:04 craigmichaelmartincraigmichaelmartin 6,5191 gold badge23 silver badges26 bronze badges 2- 3 Yes, the numeric properties are traversed in ascending order. It's explicitly described in the language spec. – Pointy Commented Aug 15, 2017 at 19:06
-
btw, you need to use a start value for reduce, like
a.reduce((total, item, index) => baz[index] = item * index, null);
– Nina Scholz Commented Aug 15, 2017 at 19:09
1 Answer
Reset to default 5The callback function is called for each element present in the array, in ascending order. It is not called for missing elements. (Missing elements? Yes, JavaScript handle sparse arrays)
var test = [];
test[30] = 'Test'; // sparse array, only one element defined.
test.forEach(
function(value){
console.log(value); // will only be called one time.
}
);
From the standard: ECMA-262
22.1.3.10 Array.prototype.forEach ( callbackfn [ , thisArg ] )
NOTE 1
callbackfn should be a function that accepts three arguments. forEach calls callbackfn once for each element present in the array, in ascending order. callbackfn is called only for elements of the array which actually exist; it is not called for missing elements of the array
If a thisArg parameter is provided, it will be used as the this value for each invocation of callbackfn. If it is not provided, undefined is used instead.
callbackfn is called with three arguments: the value of the element, the index of the element, and the object being traversed.
forEach does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.
When the forEach method is called with one or two arguments, the following steps are taken:
- Let O be ? ToObject(this value).
- Let len be ? ToLength(? Get(O, "length")).
- If IsCallable(callbackfn) is false, throw a TypeError exception.
- If thisArg was supplied, let T be thisArg; else let T be undefined.
- Let k be 0.
- Repeat, while k < len a. Let Pk be ! ToString(k). b. Let kPresent be ? HasProperty(O, Pk). c. If kPresent is true, then i. Let kValue be ? Get(O, Pk). ii. Perform ? Call(callbackfn, T, « kValue, k, O »). d. Increase k by 1.
- Return undefined.
Is the order of iterating through an array using one of the native methods (map, forEach, reduce, filter, etc) deterministic and guaranteed by the standard?
EG, are foo, bar, baz, and qux guaranteed to be [0, 2, 6, 12]
?
const a = [1, 2, 3, 4];
const foo = a.map((item, index) => item * index);
const bar = []; a.forEach((item, index) => bar[index] = item * index);
const baz = []; a.reduce((total, item, index) => baz[index] = item * index, 0);
const qux = []; a.filter((item, index) => qux[index] = item * index);
// etc
(These are (very) contrived examples)
Is the order of iterating through an array using one of the native methods (map, forEach, reduce, filter, etc) deterministic and guaranteed by the standard?
EG, are foo, bar, baz, and qux guaranteed to be [0, 2, 6, 12]
?
const a = [1, 2, 3, 4];
const foo = a.map((item, index) => item * index);
const bar = []; a.forEach((item, index) => bar[index] = item * index);
const baz = []; a.reduce((total, item, index) => baz[index] = item * index, 0);
const qux = []; a.filter((item, index) => qux[index] = item * index);
// etc
(These are (very) contrived examples)
Share Improve this question edited Feb 22, 2018 at 0:22 matisetorm 8538 silver badges21 bronze badges asked Aug 15, 2017 at 19:04 craigmichaelmartincraigmichaelmartin 6,5191 gold badge23 silver badges26 bronze badges 2- 3 Yes, the numeric properties are traversed in ascending order. It's explicitly described in the language spec. – Pointy Commented Aug 15, 2017 at 19:06
-
btw, you need to use a start value for reduce, like
a.reduce((total, item, index) => baz[index] = item * index, null);
– Nina Scholz Commented Aug 15, 2017 at 19:09
1 Answer
Reset to default 5The callback function is called for each element present in the array, in ascending order. It is not called for missing elements. (Missing elements? Yes, JavaScript handle sparse arrays)
var test = [];
test[30] = 'Test'; // sparse array, only one element defined.
test.forEach(
function(value){
console.log(value); // will only be called one time.
}
);
From the standard: ECMA-262
22.1.3.10 Array.prototype.forEach ( callbackfn [ , thisArg ] )
NOTE 1
callbackfn should be a function that accepts three arguments. forEach calls callbackfn once for each element present in the array, in ascending order. callbackfn is called only for elements of the array which actually exist; it is not called for missing elements of the array
If a thisArg parameter is provided, it will be used as the this value for each invocation of callbackfn. If it is not provided, undefined is used instead.
callbackfn is called with three arguments: the value of the element, the index of the element, and the object being traversed.
forEach does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.
When the forEach method is called with one or two arguments, the following steps are taken:
- Let O be ? ToObject(this value).
- Let len be ? ToLength(? Get(O, "length")).
- If IsCallable(callbackfn) is false, throw a TypeError exception.
- If thisArg was supplied, let T be thisArg; else let T be undefined.
- Let k be 0.
- Repeat, while k < len a. Let Pk be ! ToString(k). b. Let kPresent be ? HasProperty(O, Pk). c. If kPresent is true, then i. Let kValue be ? Get(O, Pk). ii. Perform ? Call(callbackfn, T, « kValue, k, O »). d. Increase k by 1.
- Return undefined.
本文标签:
版权声明:本文标题:Is the order of iteration for javascript array methods (map, forEach, reduce, etc) deterministic? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745576385a2157043.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论