From 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:43:23 -0500 Subject: initial commit --- .../global_objects/array/concat/index.html | 157 +++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 files/zh-tw/web/javascript/reference/global_objects/array/concat/index.html (limited to 'files/zh-tw/web/javascript/reference/global_objects/array/concat') diff --git a/files/zh-tw/web/javascript/reference/global_objects/array/concat/index.html b/files/zh-tw/web/javascript/reference/global_objects/array/concat/index.html new file mode 100644 index 0000000000..c8fc9a7aca --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/array/concat/index.html @@ -0,0 +1,157 @@ +--- +title: Array.prototype.concat() +slug: Web/JavaScript/Reference/Global_Objects/Array/concat +tags: + - Array + - JavaScript + - Method + - Prototype + - Reference + - 陣列 +translation_of: Web/JavaScript/Reference/Global_Objects/Array/concat +--- +
{{JSRef}}
+ +

concat() 方法被用來合併兩個或多個陣列。此方法不會改變現有的陣列,回傳一個包含呼叫者陣列本身的值,作為代替的是回傳一個新陣列。

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

語法

+ +
var new_array = old_array.concat(value1[, value2[, ...[, valueN]]])
+ +

參數

+ +
+
valueN
+
陣列以及/或者值,用來合併成一個新的陣列。請參閱下方詳細資訊描述。
+
+ +

回傳值

+ +

一個新的{{jsxref("Array","陣列")}}實體。

+ +

描述

+ +

concat 產生一個由呼叫者陣列自己的元素,以及對每一個參數按照順序,合併參數的元素(如果參數是個陣列)或者是參數自己本身(如果參數不是一個陣列)成為一個新的陣列。concat 方法不會遞迴巢狀陣列參數。

+ +

concat 方法不會改變 this 自己本身或是任何被提供當做參數的陣列,取而代之則是回傳一個淺層複製(shallow copy)包含了與原始的陣列中一樣的元素的副本。原始陣列的元素被複製到新的陣列的規則如下所示:

+ + + +
+

備註:合併(多個)陣列/(多個)值將讓原始的陣列不會被受到影響。此外,任何對新陣列(只有在元素不是物件參考的情況下)的操作都不會影響原始的陣列,反之亦然。

+
+ +

範例

+ +

合併兩個陣列

+ +

下面的程式碼為合併兩個陣列:

+ +
var alpha = ['a', 'b', 'c'];
+var numeric = [1, 2, 3];
+
+alpha.concat(numeric);
+// 結果: ['a', 'b', 'c', 1, 2, 3]
+
+ +

合併三個陣列

+ +

下面的程式碼為合併三個陣列:

+ +
var num1 = [1, 2, 3],
+    num2 = [4, 5, 6],
+    num3 = [7, 8, 9];
+
+var nums = num1.concat(num2, num3);
+
+console.log(nums);
+// 結果:[1, 2, 3, 4, 5, 6, 7, 8, 9]
+
+ +

合併值到一個陣列

+ +

下面的程式碼為合併三個值到一個陣列中:

+ +
var alpha = ['a', 'b', 'c'];
+
+var alphaNumeric = alpha.concat(1, [2, 3]);
+
+console.log(alphaNumeric);
+// 結果:['a', 'b', 'c', 1, 2, 3]
+
+ +

合併巢狀陣列

+ +

下面的程式碼為合併巢狀陣列,並證明保留了原本的參考(references):

+ +
var num1 = [[1]];
+var num2 = [2, [3]];
+
+var nums = num1.concat(num2);
+
+console.log(nums);
+// results in [[1], 2, [3]]
+
+// modify the first element of num1
+num1[0].push(4);
+
+console.log(nums);
+// results in [[1, 4], 2, [3]]
+
+ +

規格

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
規格狀態備註
{{SpecName('ES3')}}{{Spec2('ES3')}}首次定義。實作於 JavaScript 1.2。
{{SpecName('ES5.1', '#sec-15.4.4.4', 'Array.prototype.concat')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-array.prototype.concat', 'Array.prototype.concat')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-array.prototype.concat', 'Array.prototype.concat')}}{{Spec2('ESDraft')}} 
+ +

瀏覽器相容性

+ +
+ + +

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

+
+ +

參見

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