JavaScript数组copyInside()方法

下面是一个例子 数组copyWithin() 方法

null
  • 例子:

Javascript

<script>
// Input array
var array = [1, 2, 3, 4, 5, 6, 7];
// placing at index position 0 the element
// between index 3 and 6
document.write( "Array " + array.copyWithin(0, 3, 6));
</script>


输出:

Array 4, 5, 6, 4, 5, 6, 7

这个 arr.copyin() 方法将数组的一部分复制到同一个数组并返回它,而不修改其大小,即复制同一数组中数组的数组元素。

语法:

array.copyWithin(target, start, end)

参数: 该方法接受上述三个参数,如下所述:

  • 目标: 要将元素复制到的索引位置(必需)。
  • 开始: 这是可选的。开始复制元素的索引位置(默认值为0)。
  • 完: 这是可选的。停止从中复制元素的索引位置(默认值为array.length)。

返回值: 它返回修改后的数组。

上述方法的更多示例代码如下:

项目1:

Javascript

<script>
// Input array
var array = [1, 2, 3, 4, 5, 6, 7];
// Placing from index position 0 the
// Element from index 4
document.write( "Array " + array.copyWithin(0, 4));
</script>


输出:

Array 5, 6, 7, 4, 5, 6, 7

项目2:

Javascript

<script>
// Input array
var array = [1, 2, 3, 4, 5, 6, 7];
// Placing from index position 3
// The element from index 0
document.write( "Array " + array.copyWithin(3));
</script>


输出:

Array 1, 2, 3, 1, 2, 3, 4

申请: 每当我们需要将任何数组的内容复制到JavaScript中使用arr.copyWithin()元素时的同一个数组中。

Javascript

<script>
// Input array
var array = [1, 2, 3, 4, 5, 6, 7];
// Placing at index position 0 the
// Element between index 4 and 5
document.write( "Array " + array.copyWithin(0, 4, 5));
</script>


输出:

Array 5, 2, 3, 4, 5, 6, 7

支持的浏览器: JavaScript支持的浏览器 数组copyWithin() 方法如下:

  • 谷歌Chrome 45.0
  • 微软Edge 12.0
  • Mozilla Firefox 32.0
  • 歌剧32.0
  • 狩猎9
© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享