What do copyWithin() and fill() have in common?
Batch copy copyWithin()
grammar array.copyWithin(target, start, end) parameter:
Return value: Returns the copied array Code example: // Copy the first two elements of the array to the last two elements: var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.copyWithin(2, 0);//Banana,Orange,Banana,Orange // Copy the first two elements of the array to the third and fourth positions: var fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi", "Papaya"]; fruits.copyWithin(2, 0, 2); //Banana,Orange,Banana,Orange,Kiwi,Papaya Fill array method fill() The fill() method is used to replace the elements of an array with a fixed value. grammar: array.fill(value, start, end) parameter:
Return value: array Code example: //Fill "Runoob" to the last two elements of the array: var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.fill("Runoob", 2, 4); //[ "Banana", "Orange", "Runoob", "Runoob" ] // Fill the array with fixed values: var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.fill("Runoob");//Runoob,Runoob,Runoob,Runoob Regarding the calculation method of the index, both methods are the same
Code example: const zeroes = [0,0,0,0,0]; //Fill the elements with index greater than or equal to 3 with 6 zeroes.fill(6, 3);//[0,0,0,6,6] zeroes.fill(0); //Reset //Fill the elements with index greater than or equal to 1 and less than 3 with 7 zeroes.fill(7,1,3); //[0,7,7,0,0] zeroes.fill(0); //Reset //Fill elements with index greater than or equal to 1 and less than 4 with 8 //(-4+zeroes.length=1) (-1+zeroes.length=4) zeroes.fill(8,-4,-1); //[0,8,8,8,0] //Index is too low, ignore zeroes.fill(1,-10,-6);//[0,0,0,0,0] //Index too high, ignored zeroes.fill(1,10,15);//[0,0,0,0,0] //Index reversed, ignore zeroes.fill(2,4,2);//[0,0,0,0,0] //The index part is available, fill the available part zeroes.fill(4,3,10);//[0,0,0,4,4] This concludes this article on the specific use of ES6 copy and fill methods copyWithin() and fill(). For more information about ES6 copyWithin() and fill(), please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: W3C Tutorial (13): W3C WSDL Activities
>>: CSS makes tips boxes, bubble boxes, and triangles
SQL finds all duplicate records in a table 1. The...
Installation environment: CentOS7 64-bit, MySQL5....
There are two ways to install MySQL 5.7. One is t...
I have been engaged in Java web development for mo...
Table of contents 1. Foreign key constraints What...
Table of contents 1. Hash table principle 2. The ...
html <!DOCTYPE html> <html lang="en...
Table of contents A murder caused by ERR 1067 The...
This article shares the second article of using j...
I have been in contact with PHP for so long, but ...
As front-end engineers, IE must be familiar to us...
Table of contents Introduction question Design 1:...
What is em? em refers to the font height, and the ...
Table of contents Problem Description 1. Basic so...
I recently started learning database, and I feel ...