Javascript 中的 copyWithin()函数

首页 / JavaScript入门教程 / Javascript 中的 copyWithin()函数

copyWithin()方法复制数组中的数组序列,并在目标位置设置新的起点。 copyWithin()方法是可变方法,可以直接更新数组。它不会改变数组的长度,但是会更改其内容并在必要时创建新的属性。此方法具有三个参数,两个为必需参数,一个为可选参数。

copyWithin - 语法

arr.copyWithin(target)
arr.copyWithin(target, start)
arr.copyWithin(target,start,end)

copyWithin - 参数

target  - 将元素复制到的索引位置。 (需要)。

target  - 开始复制索引位置元素。 (可选的)

end       - 它是可选的。从中结束复制元素的源结束索引位置。

copyWithin - 返回值

修改后的数组。

copyWithin - 浏览器支持

Chrome 45.0
Edge 12.0
Firefox 32.0
Opera no

copyWithin - 例子1

JavaScript TypedArray copyWithin(target)方法

无涯教程网

<script type="text/javascript">
//Input array  
// JavaScript to illustrate copyWithin() method  
          var arr1= [1,2,3,4,5,6,7,8,9,10]; 
	   arr1.copyWithin(2)
//Placing from index position 2
//The element from index 0

           document.write(arr1);
// expected output: arr1 [Output:1,2,1,2,3,4,5,6,7,8]   
</script> 

输出:

1,2,1,2,3,4,5,6,7,8

copyWithin - 例子2

JavaScript TypedArray copyWithin(target,start)方法

<script type="text/javascript">
// Input array 
// JavaScript to illustrate copyWithin() method    
 var arr1= [1,2,3,4,5,6,7,8,9,10]; 
	   arr1.copyWithin(2,3)
//Placing from index  position  2
// Element from index 3

        document.write(arr1);
// expected output: arr1 [Output: 1,2,4,5,6,7,8,9,10,10]    
</script> 

输出:

1,2,4,5,6,7,8,9,10,10

copyWithin - 例子3

JavaScript TypedArray copyWithin(target,start,end)方法

<script type="text/javascript">
    //Input array
// JavaScript to illustrate copyWithin() method
  var arr1= [1,2,3,4,5,6,7,8,9,10]; 
  arr1.copyWithin(1,2,4)
// Placing at index position 1      
// Element between index 2 and 4

   document.write(arr1);
// expected output: arr1 [Output: 1,3,4,4,5,6,7,8,9,10]    
</script> 

输出:

1,3,4,4,5,6,7,8,9,10

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

深入拆解Java虚拟机 -〔郑雨迪〕

Elasticsearch核心技术与实战 -〔阮一鸣〕

编译原理之美 -〔宫文学〕

架构实战案例解析 -〔王庆友〕

跟月影学可视化 -〔月影〕

Tony Bai · Go语言第一课 -〔Tony Bai〕

深入浅出分布式技术原理 -〔陈现麟〕

React Native 新架构实战课 -〔蒋宏伟〕

AI大模型企业应用实战 -〔蔡超〕

好记忆不如烂笔头。留下您的足迹吧 :)