mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2026-02-27 17:44:35 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
<p class="hidden">{{EmbedInteractiveExample("pages/js/array-flatten.html")}}</p>
|
||||
<p class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a class="external" href="https://github.com/mdn/interactive-examples" rel="noopener">https://github.com/mdn/interactive-examples</a> and send us a pull request.</p>
|
||||
<h2 id="语法">语法</h2>
|
||||
<pre class="syntaxbox"><var>var newArray = arr</var>.flat(<var>depth</var>)</pre>
|
||||
<pre><code class="language-javascript"><var>var newArray = arr</var>.flat(<var>depth</var>)</code></pre>
|
||||
<h3 id="参数">参数</h3>
|
||||
<dl>
|
||||
<dt><code>depth</code> <span class="inlineIndicator optional optionalInline">可选</span></dt>
|
||||
@@ -14,7 +14,7 @@
|
||||
<p>一个包含将数组与子数组中所有元素的新数组。</p>
|
||||
<h2 id="示例">示例</h2>
|
||||
<h3 id="扁平化嵌套数组">扁平化嵌套数组</h3>
|
||||
<pre class="brush: js">var arr1 = [1, 2, [3, 4]];
|
||||
<pre><code class="language-javascript">var arr1 = [1, 2, [3, 4]];
|
||||
arr1.flat();
|
||||
// [1, 2, 3, 4]
|
||||
|
||||
@@ -29,16 +29,16 @@ arr3.flat(2);
|
||||
//使用 Infinity 作为深度,展开任意深度的嵌套数组
|
||||
arr3.flat(Infinity);
|
||||
// [1, 2, 3, 4, 5, 6]
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="扁平化与空项">扁平化与空项</h3>
|
||||
<p><code>flat()</code> 方法会移除数组中的空项:</p>
|
||||
<pre class="brush: js">var arr4 = [1, 2, , 4, 5];
|
||||
<pre><code class="language-javascript">var arr4 = [1, 2, , 4, 5];
|
||||
arr4.flat();
|
||||
// [1, 2, 4, 5]</pre>
|
||||
// [1, 2, 4, 5]</code></pre>
|
||||
<h2 id="替代方案">替代方案</h2>
|
||||
<p> </p>
|
||||
<h3 id="使用_reduce_与_concat">使用 <code>reduce</code> 与 <code>concat</code></h3>
|
||||
<pre class="brush: js">var arr1 = [1, 2, [3, 4]];
|
||||
<pre><code class="language-javascript">var arr1 = [1, 2, [3, 4]];
|
||||
arr1.flat();
|
||||
|
||||
// 反嵌套一层数组
|
||||
@@ -46,8 +46,8 @@ arr1.reduce((acc, val) => acc.concat(val), []);// [1, 2, 3, 4]
|
||||
|
||||
// 或使用 ...
|
||||
const flatSingle = arr => [].concat(...arr);
|
||||
</pre>
|
||||
<pre class="brush: js">// 使用 reduce、concat 和递归无限反嵌套多层嵌套的数组
|
||||
</code></pre>
|
||||
<pre><code class="language-javascript">// 使用 reduce、concat 和递归无限反嵌套多层嵌套的数组
|
||||
var arr1 = [1,2,3,[1,2,3,4, [2,3,4]]];
|
||||
|
||||
function flattenDeep(arr1) {
|
||||
@@ -55,8 +55,8 @@ function flattenDeep(arr1) {
|
||||
}
|
||||
flattenDeep(arr1);
|
||||
// [1, 2, 3, 1, 2, 3, 4, 2, 3, 4]
|
||||
</pre>
|
||||
<pre class="brush: js">// 不使用递归,使用 stack 无限反嵌套多层嵌套数组
|
||||
</code></pre>
|
||||
<pre><code class="language-javascript">// 不使用递归,使用 stack 无限反嵌套多层嵌套数组
|
||||
var arr1 = [1,2,3,[1,2,3,4, [2,3,4]]];
|
||||
function flatten(input) {
|
||||
const stack = [...input];
|
||||
@@ -75,7 +75,7 @@ function flatten(input) {
|
||||
return res.reverse();
|
||||
}
|
||||
flatten(arr1);// [1, 2, 3, 1, 2, 3, 4, 2, 3, 4]
|
||||
</pre>
|
||||
</code></pre>
|
||||
<div class="line"><span class="js source"><span class="comment double-slash js line"><span class="comment definition js punctuation"><span>//</span></span><span>=> [1, 2, 3, 4, 5, 6, 7, 8, 9]</span></span></span></div>
|
||||
<h2 id="规范">规范</h2>
|
||||
<table class="standard-table">
|
||||
|
||||
Reference in New Issue
Block a user