Specific use of ES6 array copy and fill methods copyWithin() and fill()

Specific use of ES6 array copy and fill methods copyWithin() and fill()

What do copyWithin() and fill() have in common?

  • You need to specify the range of an array instance: including the start index and excluding the end index.
  • Using this method will change the array contents but not the size of the array.

Batch copy copyWithin()

  • The copyWithin() method is used to copy elements from a specified position in an array to another specified position in the array.
  • copyWithin() shallowly copies the contents of the array within the specified range and inserts them starting at the specified index.

grammar

array.copyWithin(target, start, end)

parameter:

parameter describe
target Required. Copies to the specified target index location.
start Optional. The starting position of the element copying.
end Optional. The index position to stop copying (default is   array.length ). If it is a negative value, it means the reverse.

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:

parameter describe
value Required. The value to fill.
start Optional. Start filling the position.
end Optional. Stop filling position (default is   array.length )

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

  • The start index is used to specify the position to start filling, it is optional.
  • If no end index is provided, the array is filled to the end.
  • Negative indices are counted from the end of the array. You can also think of a negative index as the length of the array plus a positive index.
  • Out-of-array, zero-length, and reverse-direction index ranges are silently ignored.

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:
  • Detailed example of using Array.copyWithin() function in ES6

<<:  W3C Tutorial (13): W3C WSDL Activities

>>:  CSS makes tips boxes, bubble boxes, and triangles

Recommend

MySQL SQL statement to find duplicate data based on one or more fields

SQL finds all duplicate records in a table 1. The...

MySQL 5.7 installation and configuration tutorial under CentOS7 (YUM)

Installation environment: CentOS7 64-bit, MySQL5....

Briefly describe the use and description of MySQL primary key and foreign key

Table of contents 1. Foreign key constraints What...

Detailed explanation of JavaScript implementation of hash table

Table of contents 1. Hash table principle 2. The ...

Solution to mysql error when modifying sql_mode

Table of contents A murder caused by ERR 1067 The...

jQuery plugin to implement minesweeper game (2)

This article shares the second article of using j...

Solutions to MySql crash and service failure to start

I have been in contact with PHP for so long, but ...

Conditional comment style writing method and sample code

As front-end engineers, IE must be familiar to us...

What is em? Introduction and conversion method of em and px

What is em? em refers to the font height, and the ...

Several ways to set the expiration time of localStorage

Table of contents Problem Description 1. Basic so...

MySQL 5.7.17 installation graphic tutorial (windows)

I recently started learning database, and I feel ...