简体   繁体   English

javascript数组方法(map,forEach,reduce等)的迭代顺序是否确定?

[英]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参数,则它将用作每次调用callbackfnthis值。 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方法时,将执行以下步骤:

  1. Let O be ? 让O成为? ToObject(this value). ToObject(此值)。
  2. Let len be ? 让伦成为? ToLength(? Get(O, "length")). ToLength(?Get(O,“ length”))。
  3. If IsCallable(callbackfn) is false , throw a TypeError exception. 如果IsCallable(callbackfn)为false ,则抛出TypeError异常。
  4. If thisArg was supplied, let T be thisArg; 如果提供了thisArg,则让T为thisArg; else let T be undefined . 否则让T 不确定
  5. Let k be 0. 令k为0。
  6. 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。
  7. Return undefined . 返回undefined

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否有一个JavaScript库,它将缺少的标准迭代方法(filter,map,reduce,some ...)添加到Array中? - Is there a JavaScript library that adds missing standard iteration methods (filter, map, reduce, some…) to Array? 对于Array,在javascript中使用map()和reduce()而不是forEach()更有效吗? - For Array, is it more efficient to use map() & reduce() instead of forEach() in javascript? 懒惰map,JavaScript中的filter,reduce等 - Lazily map, filter, reduce, etc. in JavaScript Javascript:使用reduce / map等创建具有特定值的对象数组 - Javascript: Create array out array of objects with certain value using reduce / map etc 通过解构或forEach进行缩减-迭代和Airbnb JavaScript样式指南 - Reduce with deconstruction or forEach - Iteration and Airbnb JavaScript Style Guide JavaScript:如何将数组forEach,映射,过滤器等转换为“ for”循环? - JavaScript: How can i transpile array forEach, map, filter and etc to 'for' loop? 如何在 Javascript 中使用循环(forEach、map 等)将数组转换为对象 - How to convert array to object using loop (forEach, map, etc..) in Javascript javascript映射并减少数组列表 - javascript map and reduce array list 如何使用 js 数组方法(例如 map()、reduce()、filter() 等格式化来自服务器的响应,如下所示? - How can I format the response coming from the server as given below using js array methods such as map(), reduce(), filter() and etc? 希望通过使用Javascript map,reduce,foreach,filter方法来实现功能 - Want to achieve functionality by using Javascripts map,reduce,foreach,filter methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM