diff options
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/array')
11 files changed, 35 insertions, 35 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/fill/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/fill/index.html index 955cbc9d6a..6879a6b985 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/array/fill/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/array/fill/index.html @@ -36,13 +36,13 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/fill <h2 id="Description" name="Description">描述</h2> -<p><strong><code>fill</code></strong> 方法接受三个参数 <code>value</code>, <code>start</code> 以及 <code>end</code>.<span> <code>start</code> 和 <code>end</code> 参数是可选的, 其默认值分别为 <code>0</code> 和 <code>this</code> 对象的 <code>length </code></span>属性值。</p> +<p><strong><code>fill</code></strong> 方法接受三个参数 <code>value</code>, <code>start</code> 以及 <code>end</code>. <code>start</code> 和 <code>end</code> 参数是可选的, 其默认值分别为 <code>0</code> 和 <code>this</code> 对象的 <code>length </code>属性值。</p> -<p><span>如果 <code>start</code> 是个负数, 则开始索引会被自动计算成为 <code>length+start</code></span>, 其中<span> <code>length</code> 是 </span><code>this</code><span> 对象的 </span><code>length </code>属性值。<span>如果 </span><code>end</code><span> 是个负数, 则结束索引会被自动计算成为 </span><code>length+end</code>。</p> +<p>如果 <code>start</code> 是个负数, 则开始索引会被自动计算成为 <code>length+start</code>, 其中 <code>length</code> 是 <code>this</code> 对象的 <code>length </code>属性值。如果 <code>end</code> 是个负数, 则结束索引会被自动计算成为 <code>length+end</code>。</p> -<p><span><code><strong>fill</strong></code> 方法故意被设计成通用方法, 该方法不要求 <code>this</code> 是数组对象。</span></p> +<p><code><strong>fill</strong></code> 方法故意被设计成通用方法, 该方法不要求 <code>this</code> 是数组对象。</p> -<p><code><strong style="line-height: 1.5;">fill</strong></code><span style="line-height: 1.5;"> 方法是个可变方法, 它会改变调用它的 <code>this</code> 对象本身, 然后返回它, 而并不是返回一个副本。</span></p> +<p><code><strong style="line-height: 1.5;">fill</strong></code> 方法是个可变方法, 它会改变调用它的 <code>this</code> 对象本身, 然后返回它, 而并不是返回一个副本。</p> <p>当一个对象被传递给 <strong><code>fill</code></strong>方法的时候, 填充数组的是这个对象的引用。</p> 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 9a58426f8d..7b1e3c7143 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 @@ -37,7 +37,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/findIndex </dl> </dd> <dt><code>thisArg</code></dt> - <dd>可选。执行<code>callback</code>时作为<code style="font-size: 14px; line-height: inherit;">this</code>对象<span style="line-height: inherit;">的值.</span></dd> + <dd>可选。执行<code>callback</code>时作为<code style="font-size: 14px; line-height: inherit;">this</code>对象的值.</dd> </dl> <h3 id="返回值">返回值</h3> @@ -60,7 +60,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/findIndex <h3 id="Example_Testing_size_of_all_array_elements" name="Example:_Testing_size_of_all_array_elements">查找数组中首个质数元素的索引</h3> -<p><span style="line-height: inherit;">以下示例查找数组中素数的元素的索引(如果不存在素数,则返回-1)。</span></p> +<p>以下示例查找数组中素数的元素的索引(如果不存在素数,则返回-1)。</p> <pre class="brush: js notranslate">function isPrime(element, index, array) { var start = 2; 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 5346c65b82..2f37e8f49b 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 @@ -81,10 +81,10 @@ const flattened = arr => [].concat(...arr);</code></pre> <pre class="brush: js notranslate"><code>// 使用 reduce、concat 和递归展开无限多层嵌套的数组 var arr1 = [1,2,3,[1,2,3,4, [2,3,4]]]; </code> -<code class="language-js"><span class="keyword token">function</span> <span class="function token">flatDeep</span><span class="punctuation token">(</span><span class="parameter token">arr<span class="punctuation token">,</span> d <span class="operator token">=</span> <span class="number token">1</span></span><span class="punctuation token">)</span> <span class="punctuation token">{</span> - <span class="keyword token">return</span> d <span class="operator token">></span> <span class="number token">0</span> <span class="operator token">?</span> arr<span class="punctuation token">.</span><span class="function token">reduce</span><span class="punctuation token">(</span><span class="punctuation token">(</span><span class="parameter token">acc<span class="punctuation token">,</span> val</span><span class="punctuation token">)</span> <span class="operator token">=></span> acc<span class="punctuation token">.</span><span class="function token">concat</span><span class="punctuation token">(</span>Array<span class="punctuation token">.</span><span class="function token">isArray</span><span class="punctuation token">(</span>val<span class="punctuation token">)</span> <span class="operator token">?</span> <span class="function token">flatDeep</span><span class="punctuation token">(</span>val<span class="punctuation token">,</span> d <span class="operator token">-</span> <span class="number token">1</span><span class="punctuation token">)</span> <span class="punctuation token">:</span> val<span class="punctuation token">)</span><span class="punctuation token">,</span> <span class="punctuation token">[</span><span class="punctuation token">]</span><span class="punctuation token">)</span> - <span class="punctuation token">:</span> arr<span class="punctuation token">.</span><span class="function token">slice</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">;</span> -<span class="punctuation token">}</span><span class="punctuation token">;</span></code> +<code class="language-js">function flatDeep(arr, d = 1) { + return d > 0 ? arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? flatDeep(val, d - 1) : val), []) + : arr.slice(); +};</code> <code> flatDeep(arr1, Infinity); // [1, 2, 3, 1, 2, 3, 4, 2, 3, 4]</code></pre> 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 001ea6c5a9..50c75a73ab 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 @@ -54,13 +54,13 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/flatMap <h3 id="map_与_flatMap"><code>map()</code> 与 <code>flatMap()</code></h3> -<pre class="brush: js notranslate">var arr1 = <span class="js source"><span class="function-call js meta"><span class="js meta"><span class="brace js meta square"><span>[</span></span><span class="constant decimal js numeric"><span>1</span></span><span class="comma delimiter js meta object"><span>,</span></span><span> </span><span class="brace js meta square"><span>2, 3, 4]; +<pre class="brush: js notranslate">var arr1 = [1, 2, 3, 4]; -arr1.map(</span></span></span></span></span>x => [x * 2]<span class="js source"><span class="function-call js meta"><span class="js meta"><span class="brace js meta square"><span>);</span></span></span></span></span> +arr1.map(x => [x * 2]); // [[2], [4], [6], [8]] -arr1.flatMap(x => [x * 2]<span class="js source"><span class="function-call js meta"><span class="js meta"><span class="brace js meta square"><span>); -// [2, 4, 6, 8]</span></span></span></span></span> +arr1.flatMap(x => [x * 2]); +// [2, 4, 6, 8] // <code>only one level is flatt</code><code>ened</code> arr1.flatMap(x => [[x * 2]]); diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/length/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/length/index.html index 7b126495fb..8f17e9af1f 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/array/length/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/array/length/index.html @@ -82,25 +82,25 @@ function printEntries(arr) { <p>下面的例子中,通过数组下标遍历数组元素,并把每个元素的值修改为原值的2倍。</p> -<pre class="brush: js line-numbers language-js"><code class="language-js"><span class="keyword token">var</span> numbers <span class="operator token">=</span> <span class="punctuation token">[</span><span class="number token">1</span><span class="punctuation token">,</span> <span class="number token">2</span><span class="punctuation token">,</span> <span class="number token">3</span><span class="punctuation token">,</span> <span class="number token">4</span><span class="punctuation token">,</span> <span class="number token">5</span><span class="punctuation token">]</span><span class="punctuation token">;</span> -<span class="keyword token">var</span> length <span class="operator token">=</span> numbers<span class="punctuation token">.</span>length<span class="punctuation token">;</span> -<span class="keyword token">for</span> <span class="punctuation token">(</span><span class="keyword token">var</span> i <span class="operator token">=</span> <span class="number token">0</span><span class="punctuation token">;</span> i <span class="operator token"><</span> length<span class="punctuation token">;</span> i<span class="operator token">++</span><span class="punctuation token">)</span> <span class="punctuation token">{</span> - numbers<span class="punctuation token">[</span>i<span class="punctuation token">]</span> <span class="operator token">*</span><span class="operator token">=</span> <span class="number token">2</span><span class="punctuation token">;</span> -<span class="punctuation token">}</span> -<span class="comment token">// 遍历后的结果 [2, 4, 6, 8, 10]</span></code></pre> +<pre class="brush: js line-numbers language-js"><code class="language-js">var numbers = [1, 2, 3, 4, 5]; +var length = numbers.length; +for (var i = 0; i < length; i++) { + numbers[i] *= 2; +} +// 遍历后的结果 [2, 4, 6, 8, 10]</code></pre> <h3 id="Example:_Shortening_an_array" name="Example:_Shortening_an_array">截断数组</h3> <p>下面的例子中,如果数组长度大于 3,则把该数组的长度截断为 3 。</p> -<pre class="brush: js line-numbers language-js"><code class="language-js"><span class="keyword token">var</span> numbers <span class="operator token">=</span> <span class="punctuation token">[</span><span class="number token">1</span><span class="punctuation token">,</span> <span class="number token">2</span><span class="punctuation token">,</span> <span class="number token">3</span><span class="punctuation token">,</span> <span class="number token">4</span><span class="punctuation token">,</span> <span class="number token">5</span><span class="punctuation token">]</span><span class="punctuation token">;</span> +<pre class="brush: js line-numbers language-js"><code class="language-js">var numbers = [1, 2, 3, 4, 5]; -<span class="keyword token">if</span> <span class="punctuation token">(</span>numbers<span class="punctuation token">.</span>length <span class="operator token">></span> <span class="number token">3</span><span class="punctuation token">)</span> <span class="punctuation token">{</span> - numbers<span class="punctuation token">.</span>length <span class="operator token">=</span> <span class="number token">3</span><span class="punctuation token">;</span> -<span class="punctuation token">}</span> +if (numbers.length > 3) { + numbers.length = 3; +} -console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>numbers<span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// [1, 2, 3]</span> -console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>numbers<span class="punctuation token">.</span>length<span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// 3</span></code></pre> +console.log(numbers); // [1, 2, 3] +console.log(numbers.length); // 3</code></pre> <h2 id="规范">规范</h2> diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/of/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/of/index.html index 4636c06bdb..25e4a88a23 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/array/of/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/array/of/index.html @@ -45,8 +45,8 @@ Array(1, 2, 3); // [1, 2, 3] <h2 id="示例">示例</h2> -<pre class="brush: js" style="font-size: 14px;"><span style="line-height: 22px;">Array.of(1); // [1] -</span><span style="line-height: 22px;">Array.of(1, 2, 3); // [1, 2, 3]</span> +<pre class="brush: js" style="font-size: 14px;">Array.of(1); // [1] +Array.of(1, 2, 3); // [1, 2, 3] Array.of(undefined); // [undefined] </pre> 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 75edc90a73..f356bceaf6 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 @@ -79,7 +79,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/Reduce <li><code>array 数组</code></li> </ul> -<p>回调函数第一次执行时,<code>accumulator</code><span style="line-height: 1.5;"> 和</span><code>currentValue</code>的取值有两种情况:如果调用<code>reduce()</code>时提供了<code>initialValue</code>,<code>accumulator</code>取值为<code>initialValue</code>,<code>currentValue</code>取数组中的第一个值;如果没有提供 <code>initialValue</code>,那么<code>accumulator</code>取数组中的第一个值,<code>currentValue</code>取数组中的第二个值。</p> +<p>回调函数第一次执行时,<code>accumulator</code> 和<code>currentValue</code>的取值有两种情况:如果调用<code>reduce()</code>时提供了<code>initialValue</code>,<code>accumulator</code>取值为<code>initialValue</code>,<code>currentValue</code>取数组中的第一个值;如果没有提供 <code>initialValue</code>,那么<code>accumulator</code>取数组中的第一个值,<code>currentValue</code>取数组中的第二个值。</p> <div class="note"> <p><strong>注意:</strong>如果没有提供<code>initialValue</code>,reduce 会从索引1的地方开始执行 callback 方法,跳过第一个索引。如果提供<code>initialValue</code>,从索引0开始。</p> 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 e4846dbfbf..94ef4c7602 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 @@ -29,9 +29,9 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/ReduceRight <dd>一个回调函数,用于操作数组中的每个元素,它可接受四个参数: <dl> <dt><code>accumulator</code></dt> - <dd><span class="tlid-translation translation" lang="zh-CN"><span title="">累加器:</span></span>上一次调用回调函数时,回调函数返回的值。首次调用回调函数时,如果 <code>initialValue</code> 存在,<span class="tlid-translation translation" lang="zh-CN"><span title="">累加器</span></span>即为 <code>initialValue</code>,否则须为数组中的最后一个元素(详见下方 <code>initialValue</code> 处相关说明)。</dd> + <dd>累加器:上一次调用回调函数时,回调函数返回的值。首次调用回调函数时,如果 <code>initialValue</code> 存在,累加器即为 <code>initialValue</code>,否则须为数组中的最后一个元素(详见下方 <code>initialValue</code> 处相关说明)。</dd> <dt><code>currentValue</code></dt> - <dd><span class="tlid-translation translation" lang="zh-CN"><span title="">当前元素:</span></span>当前被处理的元素。</dd> + <dd>当前元素:当前被处理的元素。</dd> <dt><code>index</code>{{optional_inline}}</dt> <dd>数组中当前被处理的元素的索引。</dd> <dt><code>array</code>{{optional_inline}}</dt> @@ -39,7 +39,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/ReduceRight </dl> </dd> <dt><code>initialValue</code>{{optional_inline}} </dt> - <dd><span class="tlid-translation translation" lang="zh-CN"><span title="">首次调用</span></span> <code>callback</code> 函数时,<span class="tlid-translation translation" lang="zh-CN"><span title="">累加器 </span></span> <code>accumulator</code> <span class="tlid-translation translation" lang="zh-CN"><span title="">的值</span></span>。如果未提供该初始值,则将使用数组中的最后一个元素,并跳过该元素。如果不给出初始值,则需保证数组不为空。<br> + <dd>首次调用 <code>callback</code> 函数时,累加器 <code>accumulator</code> 的值。如果未提供该初始值,则将使用数组中的最后一个元素,并跳过该元素。如果不给出初始值,则需保证数组不为空。<br> 否则,在空数组上调用 <code>reduce</code> 或 <code>reduceRight</code> 且未提供初始值(例如 <code>[].reduce( (acc, cur, idx, arr) => {} )</code> )的话,会导致类型错误 <code><a href="/zh-CN/docs/Web/JavaScript/Reference/Errors/Reduce_of_empty_array_with_no_initial_value">TypeError: reduce of empty array with no initial value</a></code>。</dd> <dt> <h3 id="返回值">返回值</h3> diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/some/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/some/index.html index 426e6b16a6..c6e408edd2 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/array/some/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/array/some/index.html @@ -50,7 +50,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/some <p><code>some()</code> 为数组中的每一个元素执行一次 <code>callback</code> 函数,直到找到一个使得 callback 返回一个“真值”(即可转换为布尔值 true 的值)。如果找到了这样一个值,<code>some()</code> 将会立即返回 <code>true</code>。否则,<code>some()</code> 返回 <code>false</code>。<code>callback</code> 只会在那些”有值“的索引上被调用,不会在那些被删除或从来未被赋值的索引上调用。</p> -<p><code style="font-style: normal; line-height: 1.5;">callback</code><span style="line-height: 1.5;"> 被调用时传入三个参数:元素的值,元素的索引,被遍历的数组。</span></p> +<p><code style="font-style: normal; line-height: 1.5;">callback</code> 被调用时传入三个参数:元素的值,元素的索引,被遍历的数组。</p> <p>如果一个<code>thisArg</code>参数提供给some(),它将被用作调用的 <code>callback</code>的 <code>this</code> 值。否则, 它的 <code>this</code> value将是 <code>undefined</code>。<code>this</code>的值最终通过callback来观察,根据 <a href="/en-US/docs/Web/JavaScript/Reference/Operators/this">the usual rules for determining the <code>this</code> seen by a function</a>的this判定规则来确定。</p> diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/tostring/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/tostring/index.html index 9d49798a94..2378e078f8 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/array/tostring/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/array/tostring/index.html @@ -30,7 +30,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/toString <h3 id="ECMAScript_5_semantics">ECMAScript 5 semantics</h3> -<p>从 JavaScript 1.8.5 (<span class="st">Firefox 4</span>) 开始,和 ECMAScript 第5版语义(semantics)一致,<code>toString()</code> 方法是通用的,可被用于任何对象。将调用{{jsxref("Object.prototype.toString()")}},并返回结果值。</p> +<p>从 JavaScript 1.8.5 (Firefox 4) 开始,和 ECMAScript 第5版语义(semantics)一致,<code>toString()</code> 方法是通用的,可被用于任何对象。将调用{{jsxref("Object.prototype.toString()")}},并返回结果值。</p> <h2 id="规范">规范</h2> diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/unshift/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/unshift/index.html index bac11d8233..b05c74e64f 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/array/unshift/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/array/unshift/index.html @@ -40,7 +40,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Array/unshift <p><code>unshift</code> 方法会在调用它的类数组对象的开始位置插入给定的参数。</p> -<p><code>unshift</code> 特意被设计成具有通用性;这个方法能够通过 {{jsxref("Function.call", "call")}} 或 {{jsxref("Function.apply", "apply")}} 方法作用于类数组对象上。<span style="line-height: inherit;">不过对于没有 length 属性(代表从0开始的一系列连续的数字属性的最后一个)的对象,调用该方法可能没有任何意义。</span></p> +<p><code>unshift</code> 特意被设计成具有通用性;这个方法能够通过 {{jsxref("Function.call", "call")}} 或 {{jsxref("Function.apply", "apply")}} 方法作用于类数组对象上。不过对于没有 length 属性(代表从0开始的一系列连续的数字属性的最后一个)的对象,调用该方法可能没有任何意义。</p> <p>注意, 如果传入多个参数,它们会被以块的形式插入到对象的开始位置,它们的顺序和被作为参数传入时的顺序一致。 于是,传入多个参数调用一次 <code>unshift</code> ,和传入一个参数调用多次 <code>unshift</code> (例如,循环调用),它们将得到不同的结果。例如:</p> |