关于javascript:concat方法

37次阅读

共计 665 个字符,预计需要花费 2 分钟才能阅读完成。

作用:

用于连贯两个或多个数组、字符串

语法:

arrayObject.concat(arrayX,arraryX,…,arrayX)

  • arrayX,该参数能够是具体的值,也能够是数组对象,能够是任意多个

实例一:连贯两个数组

<script type="text/javascript">

  var a = [1,2,3];
  console.log(`a.concat(4,5)`);
  // 输入:1,2,3,4,5

</script>

实例二:连贯多个数组

<script type="text/javascript">

  var arr = new Array(3)
  arr[0] = "George"
  arr[1] = "John"
  arr[2] = "Thomas"

  var arr2 = new Array(3)
  arr2[0] = "James"
  arr2[1] = "Adrew"
  arr2[2] = "Martin"

  var arr3 = new Array(2)
  arr3[0] = "William"
  arr3[1] = "Franklin"

  console.log(`arr.concat(arr2,arr3)`)
  // 输入:George,John,Thomas,James,Adrew,Martin,William,Franklin
</script>

实例三: 连贯两个字符串

<script type="text/javascript">

  var txt1 = "Hello";
  var txt2 = "world!";
  var n=txt1.concat(txt2);
  console.log(n)
  // 输入:Hello world!
  
</script>

正文完
 0