日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

数组copyWithin()方法以及JavaScript中的示例

發(fā)布時間:2025/3/11 javascript 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数组copyWithin()方法以及JavaScript中的示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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()方法用于從數(shù)組中復(fù)制指定的元素,并從同一數(shù)組中的指定索引進(jìn)行替換。 它將更改此數(shù)組(實際數(shù)組)。

Syntax:

句法:

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

Parameters:

參數(shù):

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

    target_index是同一數(shù)組中用于替換元素的索引。

  • 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是一個可選參數(shù),默認(rèn)值為0,用于指定要復(fù)制元素的源起始索引。

  • 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也是一個可選參數(shù),默認(rèn)值為array.lentgh ,用于指定源結(jié)束索引。

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 indexnames.copyWithin(2);document.write("After function call...<br>");document.write("names: " + names + "<br>"); //another examplevar 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 indexarr.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

總結(jié)

以上是生活随笔為你收集整理的数组copyWithin()方法以及JavaScript中的示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。