From 01b0e12ba27b5069248fd09235e9a7143915ee30 Mon Sep 17 00:00:00 2001 From: Irvin Date: Wed, 16 Feb 2022 02:02:49 +0800 Subject: remove `notranslate` class in zh-CN --- .../global_objects/array/concat/index.html | 10 +++--- .../global_objects/array/every/index.html | 8 ++--- .../global_objects/array/filter/index.html | 12 +++---- .../global_objects/array/findindex/index.html | 6 ++-- .../reference/global_objects/array/flat/index.html | 18 +++++----- .../global_objects/array/flatmap/index.html | 10 +++--- .../reference/global_objects/array/from/index.html | 18 +++++----- .../reference/global_objects/array/map/index.html | 24 ++++++------- .../global_objects/array/reduce/index.html | 40 +++++++++++----------- .../global_objects/array/reduceright/index.html | 20 +++++------ .../global_objects/array/slice/index.html | 14 ++++---- .../global_objects/array/values/index.html | 12 +++---- 12 files changed, 96 insertions(+), 96 deletions(-) (limited to 'files/zh-cn/web/javascript/reference/global_objects/array') diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/concat/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/concat/index.html index 4541912a76..b0960c35d0 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/array/concat/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/array/concat/index.html @@ -16,7 +16,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/concat

语法

-
var new_array = old_array.concat(value1[, value2[, ...[, valueN]]])
+
var new_array = old_array.concat(value1[, value2[, ...[, valueN]]])

参数

@@ -53,7 +53,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/concat

以下代码将两个数组合并为一个新数组:

-
var alpha = ['a', 'b', 'c'];
+
var alpha = ['a', 'b', 'c'];
 var numeric = [1, 2, 3];
 
 alpha.concat(numeric);
@@ -63,7 +63,7 @@ alpha.concat(numeric);
 
 

以下代码将三个数组合并为一个新数组:

-
var num1 = [1, 2, 3],
+
var num1 = [1, 2, 3],
     num2 = [4, 5, 6],
     num3 = [7, 8, 9];
 
@@ -76,7 +76,7 @@ console.log(nums);
 
 

以下代码将三个值连接到数组:

-
var alpha = ['a', 'b', 'c'];
+
var alpha = ['a', 'b', 'c'];
 
 var alphaNumeric = alpha.concat(1, [2, 3]);
 
@@ -87,7 +87,7 @@ console.log(alphaNumeric);
 
 

以下代码合并数组并保留引用:

-
var num1 = [[1]];
+
var num1 = [[1]];
 var num2 = [2, [3]];
 var num3=[5,[6]];
 
diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/every/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/every/index.html
index a64c25b43d..38d4388b52 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/array/every/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/array/every/index.html
@@ -24,7 +24,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/every
 
 

语法

-
arr.every(callback(element[, index[, array]])[, thisArg])
+
arr.every(callback(element[, index[, array]])[, thisArg])

参数

@@ -68,7 +68,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/every

下例检测数组中的所有元素是否都大于 10。

-
function isBigEnough(element, index, array) {
+
function isBigEnough(element, index, array) {
   return element >= 10;
 }
 [12, 5, 8, 130, 44].every(isBigEnough);   // false
@@ -79,14 +79,14 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/every
 
 

箭头函数为上面的检测过程提供了更简短的语法。

-
[12, 5, 8, 130, 44].every(x => x >= 10); // false
+
[12, 5, 8, 130, 44].every(x => x >= 10); // false
 [12, 54, 18, 130, 44].every(x => x >= 10); // true

兼容旧环境(Polyfill)

在 ECMA-262 第 5 版时,every 被添加进 ECMA-262 标准;因此,在某些实现环境中,它尚未被支持。你可以把下面的代码放到脚本的开头来解决此问题,该代码允许在那些没有原生支持 every 的实现环境中使用它。该算法是 ECMA-262 第 5 版中指定的算法,它假定 Object 和 TypeError 拥有它们的初始值,且 fun.call 等价于 {{jsxref("Function.prototype.call")}}。

-
if (!Array.prototype.every) {
+
if (!Array.prototype.every) {
   Array.prototype.every = function(callbackfn, thisArg) {
     'use strict';
     var T, k;
diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/filter/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/filter/index.html
index c23bf5cb98..acc86a9592 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/array/filter/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/array/filter/index.html
@@ -20,7 +20,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/filter
 
 

语法

-
var newArray = arr.filter(callback(element[, index[, array]])[, thisArg])
+
var newArray = arr.filter(callback(element[, index[, array]])[, thisArg])

参数

@@ -69,7 +69,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/filter

下例使用 filter 创建了一个新数组,该数组的元素由原数组中值大于 10 的元素组成。

-
function isBigEnough(element) {
+
function isBigEnough(element) {
   return element >= 10;
 }
 var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
@@ -80,7 +80,7 @@ var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
 
 

以下示例使用 filter() 创建具有非零 id 的元素的 json。

-
var arr = [
+
var arr = [
   { id: 15 },
   { id: -1 },
   { id: 0 },
@@ -120,7 +120,7 @@ console.log('Number of Invalid Entries = ', invalidEntries);
 
 

下例使用 filter() 根据搜索条件来过滤数组内容。

-
var fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
+
var fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
 
 /**
  * Array filters items based on search criteria (query)
@@ -136,7 +136,7 @@ console.log(filterItems('an')); // ['banana', 'mango', 'orange']

ES2015 实现

-
const fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
+
const fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];
 
 /**
  * Array filters items based on search criteria (query)
@@ -156,7 +156,7 @@ console.log(filterItems('an')); // ['banana', 'mango', 'orange']
 
 

filter 被添加到 ECMA-262 标准第 5 版中,因此在某些实现环境中不被支持。可以把下面的代码插入到脚本的开头来解决此问题,该代码允许在那些没有原生支持 filter 的实现环境中使用它。该算法是 ECMA-262 第 5 版中指定的算法,假定 fn.call 等价于 {{jsxref("Function.prototype.call")}} 的初始值,且 {{jsxref("Array.prototype.push")}} 拥有它的初始值。

-
if (!Array.prototype.filter){
+
if (!Array.prototype.filter){
   Array.prototype.filter = function(func, thisArg) {
     'use strict';
     if ( ! ((typeof func === 'Function' || typeof func === 'function') && this) )
diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/findindex/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/findindex/index.html
index 7b1e3c7143..b0c8967e8e 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/array/findindex/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/array/findindex/index.html
@@ -20,7 +20,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/findIndex
 
 

语法

-
arr.findIndex(callback[, thisArg])
+
arr.findIndex(callback[, thisArg])

参数

@@ -62,7 +62,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/findIndex

以下示例查找数组中素数的元素的索引(如果不存在素数,则返回-1)。

-
function isPrime(element, index, array) {
+
function isPrime(element, index, array) {
   var start = 2;
   while (start <= Math.sqrt(element)) {
     if (element % start++ < 1) {
@@ -77,7 +77,7 @@ console.log([4, 6, 7, 12].findIndex(isPrime)); // 2

Polyfill

-
// https://tc39.github.io/ecma262/#sec-array.prototype.findIndex
+
// https://tc39.github.io/ecma262/#sec-array.prototype.findIndex
 if (!Array.prototype.findIndex) {
   Object.defineProperty(Array.prototype, 'findIndex', {
     value: function(predicate) {
diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/flat/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/flat/index.html
index 2f37e8f49b..f6a9420e21 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/array/flat/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/array/flat/index.html
@@ -19,7 +19,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/flat
 
 

语法

-
var newArray = arr.flat([depth])
+
var newArray = arr.flat([depth])

参数

@@ -36,7 +36,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/flat

扁平化嵌套数组

-
var arr1 = [1, 2, [3, 4]];
+
var arr1 = [1, 2, [3, 4]];
 arr1.flat();
 // [1, 2, 3, 4]
 
@@ -57,7 +57,7 @@ arr4.flat(Infinity);
 
 

flat() 方法会移除数组中的空项:

-
var arr4 = [1, 2, , 4, 5];
+
var arr4 = [1, 2, , 4, 5];
 arr4.flat();
 // [1, 2, 4, 5]
@@ -65,7 +65,7 @@ arr4.flat();

使用 reduceconcat

-
var arr = [1, 2, [3, 4]];
+
var arr = [1, 2, [3, 4]];
 
 // 展开一层数组
 arr.flat();
@@ -78,7 +78,7 @@ const flattened = arr => [].concat(...arr);

reduce + concat + isArray + recursivity

-
// 使用 reduce、concat 和递归展开无限多层嵌套的数组
+
// 使用 reduce、concat 和递归展开无限多层嵌套的数组
 var arr1 = [1,2,3,[1,2,3,4, [2,3,4]]];
 
 function flatDeep(arr, d = 1) {
@@ -91,7 +91,7 @@ flatDeep(arr1, Infinity);
 
 

forEach+isArray+push+recursivity

-
// forEach 遍历数组会自动跳过空元素
+
// forEach 遍历数组会自动跳过空元素
 const eachFlat = (arr = [], depth = 1) => {
   const result = []; // 缓存递归结果
   // 开始递归
@@ -131,7 +131,7 @@ const forFlat = (arr = [], depth = 1) => {
 
 

使用堆栈stack

-
// 无递归数组扁平化,使用堆栈
+
// 无递归数组扁平化,使用堆栈
 // 注意:深度的控制比较低效,因为需要检查每一个值的深度
 // 也可能在 shift / unshift 上进行 w/o 反转,但是末端的数组 OPs 更快
 var arr1 = [1,2,3,[1,2,3,4, [2,3,4]]];
@@ -153,7 +153,7 @@ function flatten(input) {
 }
 flatten(arr1);// [1, 2, 3, 1, 2, 3, 4, 2, 3, 4]
-
// 递归版本的反嵌套
+
// 递归版本的反嵌套
 function flatten(array) {
   var flattend = [];
   (function flat(array) {
@@ -167,7 +167,7 @@ function flatten(array) {
 
 

Use Generator function

-
function* flatten(array) {
+
function* flatten(array) {
     for (const item of array) {
         if (Array.isArray(item)) {
             yield* flatten(item);
diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/flatmap/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/flatmap/index.html
index 50c75a73ab..8383d4be6a 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/array/flatmap/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/array/flatmap/index.html
@@ -19,7 +19,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/flatMap
 
 

语法

-
var new_array = arr.flatMap(function callback(currentValue[, index[, array]]) {
+
var new_array = arr.flatMap(function callback(currentValue[, index[, array]]) {
     // return element for new_array
 }[, thisArg])
@@ -54,7 +54,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/flatMap

map() 与 flatMap()

-
var arr1 = [1, 2, 3, 4];
+
var arr1 = [1, 2, 3, 4];
 
 arr1.map(x => [x * 2]);
 // [[2], [4], [6], [8]]
@@ -70,7 +70,7 @@ arr1.flatMap(x => [[x * 2]]);
 
 

所以,为了更好的展示 flatMap 的作用,下面我们将包含几句话的数组拆分成单个词组成的新数组。

-
let arr1 = ["it's Sunny in", "", "California"];
+
let arr1 = ["it's Sunny in", "", "California"];
 
 arr1.map(x => x.split(" "));
 // [["it's","Sunny","in"],[""],["California"]]
@@ -84,7 +84,7 @@ arr1.flatMap(x => x.split(" "));
 
 

flatMap 能用于在map期间增删项目(也就是修改items的数量)。换句话说,它允许你遍历很多项使之成为另一些项(靠分别把它们放进去来处理),而不是总是一对一。 从这个意义上讲,它的作用类似于 filter的对立面。只需返回一个1项元素数组以保留该项,返回一个多元素数组以添加项,或返回一个0项元素数组以删除该项。

-
// Let's say we want to remove all the negative numbers and split the odd numbers into an even number and a 1
+
// Let's say we want to remove all the negative numbers and split the odd numbers into an even number and a 1
 let a = [5, 4, -3, 20, 17, -33, -4, 18]
 //       |\  \  x   |  | \   x   x   |
 //      [4,1, 4,   20, 16, 1,       18]
@@ -101,7 +101,7 @@ a.flatMap( (n) =>
 
 

reduce() 与 concat()

-
var arr = [1, 2, 3, 4];
+
var arr = [1, 2, 3, 4];
 
 arr.flatMap(x => [x, x * 2]);
 // is equivalent to
diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/from/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/from/index.html
index 8cab100d7c..01b6062f91 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/array/from/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/array/from/index.html
@@ -22,7 +22,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/from
 
 

语法

-
Array.from(arrayLike[, mapFn[, thisArg]])
+
Array.from(arrayLike[, mapFn[, thisArg]])
 

参数

@@ -59,18 +59,18 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/from

String 生成数组

-
Array.from('foo');
+
Array.from('foo');
 // [ "f", "o", "o" ]

Set 生成数组

-
const set = new Set(['foo', 'bar', 'baz', 'foo']);
+
const set = new Set(['foo', 'bar', 'baz', 'foo']);
 Array.from(set);
 // [ "foo", "bar", "baz" ]

Map 生成数组

-
const map = new Map([[1, 2], [2, 4], [4, 8]]);
+
const map = new Map([[1, 2], [2, 4], [4, 8]]);
 Array.from(map);
 // [[1, 2], [2, 4], [4, 8]]
 
@@ -84,7 +84,7 @@ Array.from(mapper.keys());
 
 

从类数组对象(arguments)生成数组

-
function f() {
+
function f() {
   return Array.from(arguments);
 }
 
@@ -94,7 +94,7 @@ f(1, 2, 3);
 
 

Array.from 中使用箭头函数

-
// Using an arrow function as the map function to
+
// Using an arrow function as the map function to
 // manipulate the elements
 Array.from([1, 2, 3], x => x + x);
 // [2, 4, 6]
@@ -109,7 +109,7 @@ Array.from({length: 5}, (v, i) => i);
 
 

序列生成器(指定范围)

-
// Sequence generator function (commonly referred to as "range", e.g. Clojure, PHP etc)
+
// Sequence generator function (commonly referred to as "range", e.g. Clojure, PHP etc)
 const range = (start, stop, step) => Array.from({ length: (stop - start) / step + 1}, (_, i) => start + (i * step));
 
 // Generate numbers range 0..4
@@ -127,7 +127,7 @@ range('A'.charCodeAt(0), 'Z'.charCodeAt(0), 1).map(x => String.fromCharCode(x
 
 

数组去重合并

-
function combine(){
+
function combine(){
     let arr = [].concat.apply([], arguments);  //没有去重复的新数组
     return Array.from(new Set(arr));
 }
@@ -139,7 +139,7 @@ console.log(combine(m,n));                     // [1, 2, 3]

ECMA-262 第六版标准中添加了 Array.from 。有些实现中可能尚未包括在其中。你可以通过在脚本前添加如下内容作为替代方法,以使用未原生支持的 Array.from 方法。该算法按照 ECMA-262 第六版中的规范实现,并假定 ObjectTypeError 有其本身的值, callback.call 对应 {{jsxref("Function.prototype.call")}} 。此外,鉴于无法使用 Polyfill 实现真正的的迭代器,该实现不支持规范中定义的泛型可迭代元素。

-
// Production steps of ECMA-262, Edition 6, 22.1.2.1
+
// Production steps of ECMA-262, Edition 6, 22.1.2.1
 if (!Array.from) {
   Array.from = (function () {
     var toStr = Object.prototype.toString;
diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/map/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/map/index.html
index b00ce5df07..1517622c81 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/array/map/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/array/map/index.html
@@ -20,7 +20,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/map
 
 

语法

-
var new_array = arr.map(function callback(currentValue[, index[, array]]) {
+
var new_array = arr.map(function callback(currentValue[, index[, array]]) {
  // Return element for new_array 
 }[, thisArg])
@@ -68,7 +68,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/map

下面的代码创建了一个新数组,值为原数组中对应数字的平方根。

-
var numbers = [1, 4, 9];
+
var numbers = [1, 4, 9];
 var roots = numbers.map(Math.sqrt);
 // roots的值为[1, 2, 3], numbers的值仍为[1, 4, 9]
@@ -76,7 +76,7 @@ var roots = numbers.map(Math.sqrt);

以下代码使用一个包含对象的数组来重新创建一个格式化后的数组。

-
var kvArray = [{key: 1, value: 10},
+
var kvArray = [{key: 1, value: 10},
                {key: 2, value: 20},
                {key: 3, value: 30}];
 
@@ -98,7 +98,7 @@ var reformattedArray = kvArray.map(function(obj) {
 
 

下面的代码表示了当函数需要一个参数时map的工作方式。当map循环遍历原始数组时,这个参数会自动被分配成数组中对应的每个元素。

-
var numbers = [1, 4, 9];
+
var numbers = [1, 4, 9];
 var doubles = numbers.map(function(num) {
   return num * 2;
 });
@@ -110,7 +110,7 @@ var doubles = numbers.map(function(num) {
 
 

下面的例子演示如何在一个 {{jsxref("String")}}  上使用 map 方法获取字符串中每个字符所对应的 ASCII 码组成的数组:

-
var map = Array.prototype.map
+
var map = Array.prototype.map
 var a = map.call("Hello World", function(x) {
   return x.charCodeAt(0);
 })
@@ -121,7 +121,7 @@ var a = map.call("Hello World", function(x) {
 
 

下面代码展示了如何去遍历用 querySelectorAll 得到的动态对象集合。在这里,我们获得了文档里所有选中的选项,并将其打印:

-
var elems = document.querySelectorAll('select option:checked');
+
var elems = document.querySelectorAll('select option:checked');
 var values = Array.prototype.map.call(elems, function(obj) {
   return obj.value;
 });
@@ -135,7 +135,7 @@ var values = Array.prototype.map.call(elems, function(obj) {
 
 

考虑下例:

-
["1", "2", "3"].map(parseInt);
+
["1", "2", "3"].map(parseInt);

我们期望输出 [1, 2, 3], 而实际结果是 [1, NaN, NaN].

@@ -149,14 +149,14 @@ var values = Array.prototype.map.call(elems, function(obj) {

第三个参数被parseInt忽视了, but not the second one, 但不是第二个。因此可能出现混淆。下面是迭代步骤的简明示例:

-
// parseInt(string, radix) -> map(parseInt(value, index))
+
// parseInt(string, radix) -> map(parseInt(value, index))
 /*  first iteration (index is 0): */ parseInt("1", 0); // 1
 /* second iteration (index is 1): */ parseInt("2", 1); // NaN
 /*  third iteration (index is 2): */ parseInt("3", 2); // NaN

下面让我们来讨论解决方案:

-
function returnInt(element) {
+
function returnInt(element) {
   return parseInt(element, 10);
 }
 
@@ -176,7 +176,7 @@ var values = Array.prototype.map.call(elems, function(obj) {
 
 

一个map方法调用 parseInt 作为一个参数的等效输出运行如下:

-
var xs = ['10', '10', '10'];
+
var xs = ['10', '10', '10'];
 
 xs = xs.map(parseInt);
 
@@ -187,7 +187,7 @@ console.log(xs);  // 输出结果为(3) [10, NaN, 2]
 
 

当返回undefined 或没有返回任何内容时:

-
var numbers = [1, 2, 3, 4];
+
var numbers = [1, 2, 3, 4];
 var filteredNumbers = numbers.map(function(num, index) {
   if(index < 3) {
      return num;
@@ -201,7 +201,7 @@ var filteredNumbers = numbers.map(function(num, index) {
 
 

map was added to the ECMA-262 standard in the 5th edition; as such it may not be present in all implementations of the standard. You can work around this by inserting the following code at the beginning of your scripts, allowing use of map in implementations which do not natively support it. This algorithm is exactly the one specified in ECMA-262, 5th edition, assuming {{jsxref("Object")}}, {{jsxref("TypeError")}}, and {{jsxref("Array")}} have their original values and that callback.call evaluates to the original value of {{jsxref("Function.prototype.call")}}.

-
// Production steps of ECMA-262, Edition 5, 15.4.4.19
+
// Production steps of ECMA-262, Edition 5, 15.4.4.19
 // Reference: http://es5.github.io/#x15.4.4.19
 if (!Array.prototype.map) {
 
diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/reduce/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/reduce/index.html
index f356bceaf6..cc217e28af 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/array/reduce/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/array/reduce/index.html
@@ -36,7 +36,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/Reduce
 
 

语法

-
arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
+
arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])

参数

@@ -89,7 +89,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/Reduce

提供初始值通常更安全,正如下面的例子,如果没有提供initialValue,则可能有四种输出:

-
var maxCallback = ( acc, cur ) => Math.max( acc.x, cur.x );
+
var maxCallback = ( acc, cur ) => Math.max( acc.x, cur.x );
 var maxCallback2 = ( max, cur ) => Math.max( max, cur );
 
 // reduce() 没有初始值
@@ -107,7 +107,7 @@ var maxCallback2 = ( max, cur ) => Math.max( max, cur );
 
 

假如运行下段reduce()代码:

-
[0, 1, 2, 3, 4].reduce(function(accumulator, currentValue, currentIndex, array){
+
[0, 1, 2, 3, 4].reduce(function(accumulator, currentValue, currentIndex, array){
   return accumulator + currentValue;
 });
 
@@ -172,11 +172,11 @@ var maxCallback2 = ( max, cur ) => Math.max( max, cur );

你还可以使用{{jsxref("Functions/Arrow_functions", "箭头函数","",1)}}来代替完整的函数。 下面的代码将产生与上面的代码相同的输出:

-
[0, 1, 2, 3, 4].reduce((prev, curr) => prev + curr );
+
[0, 1, 2, 3, 4].reduce((prev, curr) => prev + curr );

如果你打算提供一个初始值作为reduce()方法的第二个参数,以下是运行过程及结果:

-
[0, 1, 2, 3, 4].reduce((accumulator, currentValue, currentIndex, array) => {
+
[0, 1, 2, 3, 4].reduce((accumulator, currentValue, currentIndex, array) => {
     return accumulator + currentValue
 }, 10)
 
@@ -332,14 +332,14 @@ var maxCallback2 = ( max, cur ) => Math.max( max, cur );

数组里所有值的和

-
var sum = [0, 1, 2, 3].reduce(function (accumulator, currentValue) {
+
var sum = [0, 1, 2, 3].reduce(function (accumulator, currentValue) {
   return accumulator + currentValue;
 }, 0);
 // 和为 6

你也可以写成箭头函数的形式:

-
var total = [ 0, 1, 2, 3 ].reduce(
+
var total = [ 0, 1, 2, 3 ].reduce(
   ( acc, cur ) => acc + cur,
   0
 );
@@ -348,7 +348,7 @@ var maxCallback2 = ( max, cur ) => Math.max( max, cur );

要累加对象数组中包含的值,必须提供初始值,以便各个item正确通过你的函数。

-
var initialValue = 0;
+
var initialValue = 0;
 var sum = [{x: 1}, {x:2}, {x:3}].reduce(function (accumulator, currentValue) {
     return accumulator + currentValue.x;
 },initialValue)
@@ -357,7 +357,7 @@ console.log(sum) // logs 6

你也可以写成箭头函数的形式:

-
var initialValue = 0;
+
var initialValue = 0;
 var sum = [{x: 1}, {x:2}, {x:3}].reduce(
     (accumulator, currentValue) => accumulator + currentValue.x
     ,initialValue
@@ -368,7 +368,7 @@ console.log(sum) // logs 6
 
 

将二维数组转化为一维

-
var flattened = [[0, 1], [2, 3], [4, 5]].reduce(
+
var flattened = [[0, 1], [2, 3], [4, 5]].reduce(
   function(a, b) {
     return a.concat(b);
   },
@@ -379,7 +379,7 @@ console.log(sum) // logs 6
 
 

你也可以写成箭头函数的形式:

-
var flattened = [[0, 1], [2, 3], [4, 5]].reduce(
+
var flattened = [[0, 1], [2, 3], [4, 5]].reduce(
  ( acc, cur ) => acc.concat(cur),
  []
 );
@@ -388,7 +388,7 @@ console.log(sum) // logs 6
 
 

计算数组中每个元素出现的次数

-
var names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice'];
+
var names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice'];
 
 var countedNames = names.reduce(function (allNames, name) {
   if (name in allNames) {
@@ -404,7 +404,7 @@ var countedNames = names.reduce(function (allNames, name) {
 
 

按属性对object分类

-
var people = [
+
var people = [
   { name: 'Alice', age: 21 },
   { name: 'Max', age: 20 },
   { name: 'Jane', age: 20 }
@@ -434,7 +434,7 @@ var groupedPeople = groupBy(people, 'age');
 
 

使用扩展运算符和initialValue绑定包含在对象数组中的数组

-
// friends - 对象数组
+
// friends - 对象数组
 // where object field "books" - list of favorite books
 var friends = [{
   name: 'Anna',
@@ -469,7 +469,7 @@ var allbooks = friends.reduce(function(prev, curr) {
 

注意: 如果你正在使用一个可以兼容{{jsxref("Set")}} 和 {{jsxref("Array.from()")}} 的环境, 你可以使用let orderedArray = Array.from(new Set(myArray)); 来获得一个相同元素被移除的数组。

-
let myArray = ['a', 'b', 'a', 'b', 'c', 'e', 'e', 'c', 'd', 'd', 'd', 'd']
+
let myArray = ['a', 'b', 'a', 'b', 'c', 'e', 'e', 'c', 'd', 'd', 'd', 'd']
 let myOrderedArray = myArray.reduce(function (accumulator, currentValue) {
   if (accumulator.indexOf(currentValue) === -1) {
     accumulator.push(currentValue)
@@ -479,7 +479,7 @@ let myOrderedArray = myArray.reduce(function (accumulator, currentValue) {
 
 console.log(myOrderedArray)
-
let arr = [1,2,1,2,3,5,4,5,3,4,4,4,4];
+
let arr = [1,2,1,2,3,5,4,5,3,4,4,4,4];
 let result = arr.sort().reduce((init, current) => {
     if(init.length === 0 || init[init.length-1] !== current) {
         init.push(current);
@@ -490,7 +490,7 @@ console.log(result); //[1,2,3,4,5]

按顺序运行Promise

-
/**
+
/**
  * Runs promises from array of functions that can return promises
  * in chained manner
  *
@@ -537,7 +537,7 @@ runPromiseInSequence(promiseArr, 10)
 
 

功能型函数管道

-
// Building-blocks to use for composition
+
// Building-blocks to use for composition
 const double = x => x + x;
 const triple = x => 3 * x;
 const quadruple = x => 4 * x;
@@ -563,7 +563,7 @@ multiply24(10); // 240
 
 

使用 reduce实现map

-
if (!Array.prototype.mapUsingReduce) {
+
if (!Array.prototype.mapUsingReduce) {
   Array.prototype.mapUsingReduce = function(callback, thisArg) {
     return this.reduce(function(mappedArray, currentValue, index, array) {
       mappedArray[index] = callback.call(thisArg, currentValue, index, array)
@@ -579,7 +579,7 @@ multiply24(10); // 240
 
 

Polyfill

-
// Production steps of ECMA-262, Edition 5, 15.4.4.21
+
// Production steps of ECMA-262, Edition 5, 15.4.4.21
 // Reference: http://es5.github.io/#x15.4.4.21
 // https://tc39.github.io/ecma262/#sec-array.prototype.reduce
 if (!Array.prototype.reduce) {
diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/reduceright/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/reduceright/index.html
index 94ef4c7602..1245486db3 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/array/reduceright/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/array/reduceright/index.html
@@ -20,7 +20,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/ReduceRight
 
 

语法

-
arr.reduceRight(callback(accumulator, currentValue[, index[, array]])[, initialValue])
+
arr.reduceRight(callback(accumulator, currentValue[, index[, array]])[, initialValue])

参数

@@ -55,7 +55,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/ReduceRight

可以像下面这样调用 reduceRight 的回调函数 callback

-
array.reduceRight(function(accumulator, currentValue, index, array) {
+
array.reduceRight(function(accumulator, currentValue, index, array) {
   // ...
 });
@@ -111,7 +111,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/ReduceRight

该函数的完整执行过程见下例:

-
[0, 1, 2, 3, 4].reduceRight(function(previousValue, currentValue, index, array) {
+
[0, 1, 2, 3, 4].reduceRight(function(previousValue, currentValue, index, array) {
     return previousValue + currentValue;
 });
 
@@ -169,7 +169,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/ReduceRight

如果提供了一个 initialValue 参数,则结果如下:

-
[0, 1, 2, 3, 4].reduceRight(function(previousValue, currentValue, index, array) {
+
[0, 1, 2, 3, 4].reduceRight(function(previousValue, currentValue, index, array) {
     return previousValue + currentValue;
 }, 10);
 
@@ -235,14 +235,14 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/ReduceRight

求一个数组中所有值的和

-
var sum = [0, 1, 2, 3].reduceRight(function(a, b) {
+
var sum = [0, 1, 2, 3].reduceRight(function(a, b) {
   return a + b;
 });
 // sum is 6

扁平化(flatten)一个二维数组

-
var flattened = [[0, 1], [2, 3], [4, 5]].reduceRight(function(a, b) {
+
var flattened = [[0, 1], [2, 3], [4, 5]].reduceRight(function(a, b) {
     return a.concat(b);
 }, []);
 // flattened is [4, 5, 2, 3, 0, 1]
@@ -250,7 +250,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/ReduceRight
 
 

运行一个带有回调每个函数将其结果传给下一个的异步函数列表

-
const waterfall = (...functions) => (callback, ...args) =>
+
const waterfall = (...functions) => (callback, ...args) =>
   functions.reduceRight(
     (composition, fn) => (...results) => fn(composition, ...results),
     callback
@@ -293,7 +293,7 @@ const computation2 = (input, callback) => {
 
 

展示 reducereduceRight 之间的区别

-
var a = ['1', '2', '3', '4', '5'];
+
var a = ['1', '2', '3', '4', '5'];
 var left  = a.reduce(function(prev, cur)      { return prev + cur; });
 var right = a.reduceRight(function(prev, cur) { return prev + cur; });
 
@@ -304,7 +304,7 @@ console.log(right); // "54321"

组合函数的概念简单,它只是简单地结合了多个函数。它是一个从右向左流动的函数,用上一个函数的输出调用每个函数。

-
/**
+
/**
  * Function Composition is way in which result of one function can
  * be passed to another and so on.
  *
@@ -333,7 +333,7 @@ console.log(compose(inc, double)(2)); // 5

reduceRight 被添加到 ECMA-262 标准第 5 版,因此它在某些实现环境中可能不被支持。把下面的代码添加到脚本开头可以解决此问题,从而允许在那些没有原生支持 reduceRight 的实现环境中使用它。

-
// Production steps of ECMA-262, Edition 5, 15.4.4.22
+
// Production steps of ECMA-262, Edition 5, 15.4.4.22
 // Reference: http://es5.github.io/#x15.4.4.22
 if ('function' !== typeof Array.prototype.reduceRight) {
   Array.prototype.reduceRight = function(callback /*, initialValue*/) {
diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/slice/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/slice/index.html
index eb68df4907..b69293d1db 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/array/slice/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/array/slice/index.html
@@ -20,7 +20,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/slice
 
 

语法

-
arr.slice([begin[, end]])
+
arr.slice([begin[, end]])

参数

@@ -63,7 +63,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/slice

返回现有数组的一部分

-
var fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
+
var fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango'];
 var citrus = fruits.slice(1, 3);
 
 // fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
@@ -78,7 +78,7 @@ var citrus = fruits.slice(1, 3);
 
 

在下例中, slicemyCar 中创建了一个新数组newCar。两个数组都包含了一个 myHonda 对象的引用。当 myHondacolor 属性改变为 purple,则两个数组中的对应元素都会随之改变。

-
// 使用 slice 方法从 myCar 中创建一个 newCar。
+
// 使用 slice 方法从 myCar 中创建一个 newCar。
 var myHonda = { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } };
 var myCar = [myHonda, 2, "cherry condition", "purchased 1997"];
 var newCar = myCar.slice(0, 2);
@@ -100,7 +100,7 @@ console.log('newCar[0].color = ' + newCar[0].color);
 
 

上述代码输出:

-
 myCar = [{color: 'red', wheels: 4, engine: {cylinders: 4, size: 2.2}}, 2,
+
 myCar = [{color: 'red', wheels: 4, engine: {cylinders: 4, size: 2.2}}, 2,
        'cherry condition', 'purchased 1997']
 newCar = [{color: 'red', wheels: 4, engine: {cylinders: 4, size: 2.2}}, 2]
  myCar[0].color = red
@@ -114,7 +114,7 @@ newCar[0].color = purple
 
 

slice 方法可以用来将一个类数组(Array-like)对象/集合转换成一个新数组。你只需将该方法绑定到这个对象上。 一个函数中的  {{jsxref("Functions/arguments", "arguments")}} 就是一个类数组对象的例子。

-
function list() {
+
function list() {
   return Array.prototype.slice.call(arguments);
 }
 
@@ -123,7 +123,7 @@ var list1 = list(1, 2, 3); // [1, 2, 3]
 
 

除了使用 Array.prototype.slice.call(arguments),你也可以简单的使用 [].slice.call(arguments) 来代替。另外,你可以使用 bind 来简化该过程。

-
var unboundSlice = Array.prototype.slice;
+
var unboundSlice = Array.prototype.slice;
 var slice = Function.prototype.call.bind(unboundSlice);
 
 function list() {
@@ -137,7 +137,7 @@ var list1 = list(1, 2, 3); // [1, 2, 3]
 
 

根据规范,使用 Array.prototype.slice 转换宿主对象(如 DOM 对象)时,不必遵循 Mozilla 的默认行为,即可以转化任何符合条件的伪数组宿主对象为数组,IE < 9 没有遵循,而 IE9 + 遵循这个行为,但是稍加改造可以使其在跨浏览器使用时更可靠。只要其他现代浏览器继续支持该行为,目前 IE 9+、FireFox、Chrome、Safari 以及 Opera 都支持,开发者在使用下面代码时遍历 DOM 时就不会被该方法的字面意义误导,即 IE < 9 不能转化 DOM Collections。开发者可以安全地根据语义知道该方法的实际上的标准行为。(下面的代码还修正了 IE 中 slice() 方法第二个参数不允许为显式的 {{jsxref("Global_Objects/null", "null")}}/{{jsxref("Global_Objects/undefined", "undefined")}} 值的问题,其他现代浏览器,包括 IE9+ 都允许)。

-
/**
+
/**
  * Shim for "fixing" IE's lack of support (IE < 9) for applying slice
  * on host objects like NamedNodeMap, NodeList, and HTMLCollection
  * (technically, since host objects have been implementation-dependent,
diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/values/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/values/index.html
index 7af8b40436..d8cc03f53f 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/array/values/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/array/values/index.html
@@ -20,7 +20,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/values
 
 

语法

-
arr.values()
+
arr.values()

返回值

@@ -30,7 +30,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/values

使用 for...of 循环进行迭代

-
let arr = ['w', 'y', 'k', 'o', 'p'];
+
let arr = ['w', 'y', 'k', 'o', 'p'];
 let eArr = arr.values();
 
 for (let letter of eArr) {
@@ -39,11 +39,11 @@ for (let letter of eArr) {
 
 

Array.prototype.values 是 Array.prototype[Symbol.iterator] 的默认实现。

-
Array.prototype.values === Array.prototype[Symbol.iterator]  // true 
+
Array.prototype.values === Array.prototype[Symbol.iterator]  // true 

使用 .next() 迭代

-
var arr = ['a', 'b', 'c', 'd', 'e'];
+
var arr = ['a', 'b', 'c', 'd', 'e'];
 var iterator = arr.values();
 iterator.next();               // Object { value: "a", done: false }
 iterator.next().value;         // "b"
@@ -59,7 +59,7 @@ iterator.next().value;         // undefined

例子:

-
var arr = ['a', 'b', 'c', 'd', 'e'];
+
var arr = ['a', 'b', 'c', 'd', 'e'];
  var iterator = arr.values();
  for (let letter of iterator) {
  console.log(letter);
@@ -72,7 +72,7 @@ console.log(letter);
 
 

: 数组迭代器中存储的是原数组的地址,而不是数组元素值。

-
var arr = ['a', 'b', 'c', 'd', 'e'];
+
var arr = ['a', 'b', 'c', 'd', 'e'];
 var iterator = arr.values();
 console.log(iterator); // Array Iterator {  }
 iterator.next().value; // "a"
-- 
cgit v1.2.3-54-g00ecf