From 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:43:23 -0500 Subject: initial commit --- .../reference/global_objects/array/flat/index.html | 179 +++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 files/vi/web/javascript/reference/global_objects/array/flat/index.html (limited to 'files/vi/web/javascript/reference/global_objects/array/flat') 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 +--- +
{{JSRef}}
+ +
Phương thức flat()  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.
+ + + +

{{EmbedInteractiveExample("pages/js/array-flat.html")}}

+ + + +

Syntax

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

Parameters

+ +
+
depth {{optional_inline}}
+
Chỉ định độ sâu của mảng kết quả cuối cùng. Mặc định là 1
+
+ +

Return value

+ +

Một mảng mới với các phần tử của mảng con được nối với nó.

+ +

Alternatives

+ +

reduce and concat

+ +
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) => acc.concat(val), []);
+// [1, 2, 3, 4]
+
+// hoặc decomposition syntax
+const flattened = arr => [].concat(...arr);
+
+ +

reduce + concat + isArray + recursivity

+ +
const arr = [1, 2, [3, 4, [5, 6]]];
+
+// ?Sử dụng reduce, concat và deep level
+function flatDeep(arr, d = 1) {
+   return d > 0 ? arr.reduce((acc, val) => acc.concat(Array.isArray(val) ? flatDeep(val, d - 1) : val), [])
+                : arr.slice();
+};
+
+flatDeep(arr, Infinity);
+// [1, 2, 3, 4, 5, 6]
+
+ +

Use a stack

+ +
// 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]
+
+ +

Use Generator function

+ +
function* flatten(array, depth) {
+    if(depth === undefined) {
+      depth = 1;
+    }
+    for(const item of array) {
+        if(Array.isArray(item) && depth > 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]
+
+ + + +

Examples

+ +

Flattening nested arrays

+ +
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]
+
+ +

Flattening and array holes

+ +

The flat method removes empty slots in arrays:

+ +
const arr5 = [1, 2, , 4, 5];
+arr5.flat();
+// [1, 2, 4, 5]
+
+ +

Specifications

+ + + + + + + + + + + + +
Specification
{{SpecName('ESDraft', '#sec-array.prototype.flat', 'Array.prototype.flat')}}
+ +

Browser compatibility

+ +
+ + +

{{Compat("javascript.builtins.Array.flat")}}

+
+ +

See also

+ + -- cgit v1.2.3-54-g00ecf