当前位置: 首页 > 网络学院 > 客户端脚本教程 > JavaScript > JavaScript concat() 方法

JavaScript
JS 字符串
JS Date
JS数组,JS Array
JS Boolean
JS Math
JS HTML DOM
JS Browser
JS Cookies
JS 校验
JS Animation
JS Image Maps
JS Timing
JS 建立对象
JS 摘要
JS 实例
JS 对象实例
JS DOM 实例
JS数组对象参考
JS布尔对象参考
JS日期对象参考

JavaScript concat() 方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 387 ::
收藏到网摘: n/a

Definition and Usage
定义与用法

The concat() method is used to join two or more arrays.
使用concat()方法可用来将两个或多个数组结合起来

This method does not change the existing arrays, it only returns a copy of the joined arrays.
这个方法不会改变现存的数组,它只返回了所结合数组的一份拷贝。

Syntax 语法

arrayObject.concat(arrayX,arrayX,......,arrayX)

Parameter
参数
Description
描述
arrayX Required. One or more array objects to be joined to an array
必选项。一个或多个数组组合成一个数组


举例 1

Here we create two arrays and show them as one using concat():
这里我们使用了concat()将两个数组结合成了一个:

<script type="text/javascript">
var arr = new Array(3)
arr[0] = "Jani"
arr[1] = "Tove"
arr[2] = "Hege"
var arr2 = new Array(3)
arr2[0] = "John"
arr2[1] = "Andy"
arr2[2] = "Wendy"
document.write(arr.concat(arr2))
</script>

The output of the code above will be:
上面代码的输出结果为:

Jani,Tove,Hege,John,Andy,Wendy


举例 2

Here we create three arrays and show them as one using concat():
我们通过使用concat()将三个数组结合成为了一个数组:

<script type="text/javascript">
var arr = new Array(3)
arr[0] = "Jani"
arr[1] = "Tove"
arr[2] = "Hege"
var arr2 = new Array(3)
arr2[0] = "John"
arr2[1] = "Andy"
arr2[2] = "Wendy"
var arr3 = new Array(2)
arr3[0] = "Stale"
arr3[1] = "Borge"
document.write(arr.concat(arr2,arr3))
</script>

The output of the code above will be:
上面代码的结果为:

Jani,Tove,Hege,John,Andy,Wendy,Stale,Borge


Try-It-Yourself Demos
尝试与演示

How to join two arrays with the concat() method
怎样通过concat()方法结合两个数组

How to join three arrays with the concat() method
怎样使用concat()方法结合三个数组

评论 (0) All

登陆 | 还没注册?