[英]Is the order of iteration for javascript array methods (map, forEach, reduce, etc) deterministic?
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? 使用本机方法(map,forEach,reduce,filter等)之一遍历数组的顺序是否由标准确定并保证?
EG, are foo, bar, baz, and qux guaranteed to be [0, 2, 6, 12]
? EG,foo,bar,baz和qux是否保证为[0, 2, 6, 12]
0,2,6,12 [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) (这些是(非常)人为的示例)
Generally they behave exactly like this for loop: 通常,它们的行为与for循环完全相同:
for(var i = 0; i < this.length; i++)
Theres just one exception, reduceRight iterates like this: 只有一个例外, reduceRight像这样迭代:
for(var i = this.length - 1; i+1 ; i-- )
Unlike the upper examples, they jump over not defined properties. 与上面的示例不同,它们跳过了未定义的属性。
The 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) (缺少元素?是的,JavaScript处理稀疏数组)
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 来自标准: ECMA-262
22.1.3.10 Array.prototype.forEach ( callbackfn [ , thisArg ] ) 22.1.3.10 Array.prototype.forEach(callbackfn [,thisArg])
NOTE 1 注1
callbackfn should be a function that accepts three arguments. callbackfn应该是一个接受三个参数的函数。 forEach calls callbackfn once for each element present in the array, in ascending order. forEach对数组中存在的每个元素按升序调用一次callbackfn 。 callbackfn is called only for elements of the array which actually exist; 仅对实际存在的数组元素调用callbackfn 。 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 . 如果提供了thisArg参数,则它将用作每次调用callbackfn的this值。 If it is not provided, undefined is used instead. 如果未提供,则使用undefined 。
callbackfn is called with three arguments: the value of the element, the index of the element, and the object being traversed. 使用三个参数调用callbackfn :元素的值,元素的索引和要遍历的对象。
forEach does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn . forEach不会直接改变在其上调用它的对象,但是可以通过对callbackfn的调用来改变该对象。
When the forEach method is called with one or two arguments, the following steps are taken: 使用一个或两个参数调用forEach方法时,将执行以下步骤:
- Let O be ? 让O成为? ToObject(this value). ToObject(此值)。
- Let len be ? 让伦成为? ToLength(? Get(O, "length")). ToLength(?Get(O,“ length”))。
- If IsCallable(callbackfn) is false , throw a TypeError exception. 如果IsCallable(callbackfn)为false ,则抛出TypeError异常。
- If thisArg was supplied, let T be thisArg; 如果提供了thisArg,则让T为thisArg; else let T be undefined . 否则让T 不确定 。
- Let k be 0. 令k为0。
- Repeat, while k < len a. 重复,而k <len a。 Let Pk be ! 让Pk成为! ToString(k). ToString(k)。 b. b。 Let kPresent be ? 让kPresent为? HasProperty(O, Pk). HasProperty(O,Pk)。 c. C。 If kPresent is true , then i. 如果kPresent为true ,那么我。 Let kValue be ? 令kValue为? Get(O, Pk). 获取(O,Pk)。 ii. ii。 Perform ? 执行? Call(callbackfn, T, « kValue, k, O »). Call(callbackfn,T,«kValue,k,O»)。 d. d。 Increase k by 1. 将k增加1。
- Return undefined . 返回undefined 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.