JavaScript copyWithin()方法 (JavaScript copyWithin() method)

copyWithin() method is used to copy the specified elements from an array and replace from specified index within the same array. It changes the this array (actual array).

copyWithin()方法用于从数组中复制指定的元素,并从同一数组中的指定索引进行替换。 它将更改此数组(实际数组)。

Syntax:

句法:

    array.concat(target_index, [start_index], [end_index]);

Parameters:

参数:

  • target_index is an index in the same array to replace the elements.

    target_index是同一数组中用于替换元素的索引。

  • start_index is an optional parameter and it's default value is 0, it is used to specify the source start index to copy the elements.

    start_index是一个可选参数,默认值为0,用于指定要复制元素的源起始索引。

  • end_index is also an optional parameter and it's default value is array.lentgh, it is used to specify the source end index.

    end_index也是一个可选参数,默认值为array.lentgh ,用于指定源结束索引。

Example:

例:

    Input:
var names = ["Manju", "Amit", "Abhi", "Radib", "Prem"];
Function call:
names.copyWithin(2, 0);
Output:
Manju,Amit,Manju,Amit,Abhi

JavaScript Code to demonstrate example of Array.copyWithin() method

JavaScript代码演示Array.copyWithin()方法的示例

<html>
<head>
<title>JavaScipt Example</title>
</head>
<body>
<script>
var names = ["Manju", "Amit", "Abhi", "Radib", "Prem"];
document.write("Before function call...<br>");
document.write("names: " + names + "<br>");
//copy from 0th index and replace from 2nd index
names.copyWithin(2);
document.write("After function call...<br>");
document.write("names: " + names + "<br>");
//another example
var arr = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
document.write("Before function call...<br>");
document.write("arr: " + arr + "<br>");
//copy from 1st index to 3rd and replace from 6th index
arr.copyWithin(6,1,4);
document.write("After function call...<br>");
document.write("arr: " + arr + "<br>");
</script>
</body>
</html>

Output

输出量

Before function call...
names: Manju,Amit,Abhi,Radib,Prem
After function call...
names: Manju,Amit,Manju,Amit,Abhi
Before function call...
arr: 10,20,30,40,50,60,70,80,90,100
After function call...
arr: 10,20,30,40,50,60,20,30,40,100

翻译自: https://www.includehelp.com/code-snippets/array-copyWithin-method-with-example-in-javascript.aspx

数组copyWithin()方法以及JavaScript中的示例相关推荐

  1. 数组shift方法_数组shift()方法以及JavaScript中的示例

    数组shift方法 JavaScript shift()方法 (JavaScript shift() method) shift() method is used to remove the firs ...

  2. 数组tostring方法_数组toString()方法以及JavaScript中的示例

    数组tostring方法 JavaScript toString()方法 (JavaScript toString() method) toString() method is used to con ...

  3. 数组长度属性以及JavaScript中的示例

    JavaScript长度属性 (JavaScript length property) length property is used to get and set the length of an ...

  4. find 命令示例_数组find()方法以及JavaScript中的示例

    find 命令示例 JavaScript find()方法 (JavaScript find() method) find() method is used to get the first elem ...

  5. 数组的fill方法_数组fill()方法以及JavaScript中的示例

    数组的fill方法 JavaScript fill()方法 (JavaScript fill() method) fill() method is used fill the array with a ...

  6. 数组中的reverse_数组reverse()方法以及JavaScript中的示例

    数组中的reverse JavaScript reverse()方法 (JavaScript reverse() method) reverse() method is used to reverse ...

  7. 数组中的filter方法_数组filter()方法以及JavaScript中的示例

    数组中的filter方法 JavaScript filter()方法 (JavaScript filter() method) filter() method is used to returns a ...

  8. 数组push()方法以及JavaScript中的示例

    JavaScript push()方法 (JavaScript push() method) push() method is used adds/inserts an element at the ...

  9. matlab中now函数_now()方法以及JavaScript中的示例

    matlab中now函数 JavaScript now()方法 (JavaScript now() method) now() method is a Date class method, it is ...

最新文章

  1. Pure-FTPd服务器
  2. Go实现简单的K-V存储
  3. 分享大厂分布式唯一ID设计方案,快来围观
  4. oracle hint push_pred,hints的push_pred应用-Oracle
  5. [:zh]给机械课程设计的一封信[:] 2017-12-23
  6. oracle使用parallel并行,多线程查询
  7. CSS——简写属性(在padding和margin这样的简写属性中,值赋值的顺序是top、right、bottom、left)...
  8. [hdu5628]Clarke and math(dirichlet卷积)
  9. MT4 中各种指标线的解释
  10. 写论文不会写摘要,演讲不会写开场白,不会写广告词,SCQA模型帮你搞定
  11. 远程车控怎么玩?来,我教你..
  12. 为什么要面向对象编程?
  13. UML --Package
  14. nvm介绍及常用命令
  15. 阿里云DDoS防护和DDoS高防有什么区别
  16. mysql 重做日志 镜像_mysql重做日志 - osc_vr7hvjd2的个人空间 - OSCHINA - 中文开源技术交流社区...
  17. Caused by: java.sql.SQLException: Value ‘0000-00-00‘ can not be represented as java.sql.Date
  18. 【收藏】关于元数据(Metadata)和元数据管理,这是我的见过最全的解读!
  19. MATLAB光谱特征波长提取,uve算法提取光谱特征波长
  20. 地图采集车的那些事 | 硬件质量篇

热门文章

  1. MIP开发教程(二) 使用MIP-CLI工具调试MIP网页
  2. Problem D: 顺序串的基本运算
  3. 教你如何把自己从好友的QQ中删除
  4. kafka 支持发布订阅
  5. Java分享笔记:自定义枚举类 使用enum关键字定义枚举类
  6. 中国制造2025变革,背后的大数据来龙去脉
  7. Ubuntu 忘记密码的处理方法
  8. pjtool用到的数据库----oracle范畴
  9. Qt4小技巧——QTextEdit自动滚屏
  10. mssql 2008恢复xp_cmdshell