aboutsummaryrefslogtreecommitdiff
path: root/files/vi/web/javascript/reference/global_objects/array/flat
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:43:23 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:43:23 -0500
commit218934fa2ed1c702a6d3923d2aa2cc6b43c48684 (patch)
treea9ef8ac1e1b8fe4207b6d64d3841bfb8990b6fd0 /files/vi/web/javascript/reference/global_objects/array/flat
parent074785cea106179cb3305637055ab0a009ca74f2 (diff)
downloadtranslated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.gz
translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.bz2
translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.zip
initial commit
Diffstat (limited to 'files/vi/web/javascript/reference/global_objects/array/flat')
-rw-r--r--files/vi/web/javascript/reference/global_objects/array/flat/index.html179
1 files changed, 179 insertions, 0 deletions
diff --git a/files/vi/web/javascript/reference/global_objects/array/flat/index.html b/files/vi/web/javascript/reference/global_objects/array/flat/index.html
new file mode 100644
index 0000000000..065a92a7a5
--- /dev/null
+++ b/files/vi/web/javascript/reference/global_objects/array/flat/index.html
@@ -0,0 +1,179 @@
+---
+title: Array.prototype.flat()
+slug: Web/JavaScript/Reference/Global_Objects/Array/flat
+tags:
+ - Array
+ - JavaScript
+ - Method
+ - Prototype
+ - Reference
+ - flat
+translation_of: Web/JavaScript/Reference/Global_Objects/Array/flat
+---
+<div>{{JSRef}}</div>
+
+<div>Phương thức <code><strong>flat()</strong></code>  trả về một mảng mới. Trong đó, tất cả các phần tử của mảng con được gán ngược vào nó bằng  cách đệ quy lên đến độ sâu đã chỉ định.</div>
+
+
+
+<p>{{EmbedInteractiveExample("pages/js/array-flat.html")}}</p>
+
+
+
+<h2 id="Syntax">Syntax</h2>
+
+<pre class="syntaxbox notranslate"><var>var newArray = arr</var>.flat(<em>[depth]</em>);</pre>
+
+<h3 id="Parameters">Parameters</h3>
+
+<dl>
+ <dt><code>depth</code> {{optional_inline}}</dt>
+ <dd>Chỉ định độ sâu của mảng kết quả cuối cùng. Mặc định là 1</dd>
+</dl>
+
+<h3 id="Return_value">Return value</h3>
+
+<p>Một mảng mới với các phần tử của mảng con được nối với nó.</p>
+
+<h2 id="Alternatives">Alternatives</h2>
+
+<h3 id="reduce_and_concat">reduce and concat</h3>
+
+<pre class="brush: js notranslate">const arr = [1, 2, [3, 4]];
+
+// Trả về mảng chỉ có 1 level
+arr.flat();
+
+// Nó ngang với việc sử dụng reduce
+arr.reduce((acc, val) =&gt; acc.concat(val), []);
+// [1, 2, 3, 4]
+
+// hoặc decomposition syntax
+const flattened = arr =&gt; [].concat(...arr);
+</pre>
+
+<h3 id="reduce_concat_isArray_recursivity">reduce + concat + isArray + recursivity</h3>
+
+<pre class="brush: js notranslate">const arr = [1, 2, [3, 4, [5, 6]]];
+
+// ?Sử dụng reduce, concat và deep level
+function flatDeep(arr, d = 1) {
+ return d &gt; 0 ? arr.reduce((acc, val) =&gt; acc.concat(Array.isArray(val) ? flatDeep(val, d - 1) : val), [])
+ : arr.slice();
+};
+
+flatDeep(arr, Infinity);
+// [1, 2, 3, 4, 5, 6]
+</pre>
+
+<h3 id="Use_a_stack">Use a stack</h3>
+
+<pre class="brush: js notranslate">// Sử dụng stack để đệ quy không cần báo deep level
+
+function flatten(input) {
+ const stack = [...input];
+ const res = [];
+ while(stack.length) {
+ // Lấy gía trị ra khỏi stack
+ const next = stack.pop();
+ if(Array.isArray(next)) {
+  // Gán trở lại arry, không thay đổi giá trị của item đó
+ stack.push(...next);
+ } else {
+ res.push(next);
+ }
+ }
+ // Đảo ngược array để trả về đúng order ban đầu
+ return res.reverse();
+}
+
+const arr = [1, 2, [3, 4, [5, 6]]];
+flatten(arr);
+// [1, 2, 3, 4, 5, 6]
+</pre>
+
+<h3 id="Use_Generator_function">Use Generator function</h3>
+
+<pre class="brush: js notranslate">function* flatten(array, depth) {
+ if(depth === undefined) {
+ depth = 1;
+ }
+ for(const item of array) {
+ if(Array.isArray(item) &amp;&amp; depth &gt; 0) {
+ yield* flatten(item, depth - 1);
+ } else {
+ yield item;
+ }
+ }
+}
+
+const arr = [1, 2, [3, 4, [5, 6]]];
+const flattened = [...flatten(arr, Infinity)];
+// [1, 2, 3, 4, 5, 6]
+</pre>
+
+<div class="hidden">
+<p>Please do not add polyfills on this article. For reference, please check: <a href="https://discourse.mozilla.org/t/mdn-rfc-001-mdn-wiki-pages-shouldnt-be-a-distributor-of-polyfills/24500">https://discourse.mozilla.org/t/mdn-rfc-001-mdn-wiki-pages-shouldnt-be-a-distributor-of-polyfills/24500</a></p>
+</div>
+
+<h2 id="Examples">Examples</h2>
+
+<h3 id="Flattening_nested_arrays">Flattening nested arrays</h3>
+
+<pre class="brush: js notranslate">const arr1 = [1, 2, [3, 4]];
+arr1.flat();
+// [1, 2, 3, 4]
+
+const arr2 = [1, 2, [3, 4, [5, 6]]];
+arr2.flat();
+// [1, 2, 3, 4, [5, 6]]
+
+const arr3 = [1, 2, [3, 4, [5, 6]]];
+arr3.flat(2);
+// [1, 2, 3, 4, 5, 6]
+
+const arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
+arr4.flat(Infinity);
+// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+</pre>
+
+<h3 id="Flattening_and_array_holes">Flattening and array holes</h3>
+
+<p>The flat method removes empty slots in arrays:</p>
+
+<pre class="brush: js notranslate">const arr5 = [1, 2, , 4, 5];
+arr5.flat();
+// [1, 2, 4, 5]
+</pre>
+
+<h2 id="Specifications">Specifications</h2>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">Specification</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-array.prototype.flat', 'Array.prototype.flat')}}</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility">Browser compatibility</h2>
+
+<div>
+
+
+<p>{{Compat("javascript.builtins.Array.flat")}}</p>
+</div>
+
+<h2 id="See_also">See also</h2>
+
+<ul>
+ <li>{{jsxref("Array.prototype.flatMap()")}}</li>
+ <li>{{jsxref("Array.prototype.map()")}}</li>
+ <li>{{jsxref("Array.prototype.reduce()")}}</li>
+ <li>{{jsxref("Array.prototype.concat()")}}</li>
+</ul>