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 --- .../reference/global_objects/array/map/index.html | 24 +++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'files/zh-cn/web/javascript/reference/global_objects/array/map') 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) {
 
-- 
cgit v1.2.3-54-g00ecf