gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:32:02 28 4
gpt4 key购买 nike

使用一种 native 方法(map、forEach、reduce、filter 等)遍历数组的顺序是否确定并由标准保证?

EG,foo、bar、baz 和 qux 是否保证为 [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

(这些是(非常)人为的例子)

最佳答案

为数组中存在的每个元素按升序调用回调函数。它为缺少的元素调用。 (缺少元素?是的,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.
}
);

来自标准: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:

  1. Let O be ? ToObject(this value).
  2. Let len be ? ToLength(? Get(O, "length")).
  3. If IsCallable(callbackfn) is false, throw a TypeError exception.
  4. If thisArg was supplied, let T be thisArg; else let T be undefined.
  5. Let k be 0.
  6. 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.
  7. Return undefined.

关于javascript - javascript 数组方法(map、forEach、reduce 等)的迭代顺序是否确定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45699754/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com