ES6 Array Copy and Fill Method copyWithin of fill of Specific Use

  • 2021-11-29 23:06:43
  • OfStack

Directory Bulk Copy copyWithin () Fill Array Method fill () As for the calculation method of index, the two methods are the same

copyWithin () and fill () have something in common

You need to specify the scope of 1 array instance: including the starting index, but not the ending index. Using this method changes the contents of the array, but does not change the size of the array.

Bulk copy copyWithin ()

The copyWithin () method is used to copy elements from the specified location in the array to another specified location in the array. copyWithin () shallowly copies parts of the array in the specified range and inserts them at the beginning of the specified index.

Grammar


array.copyWithin(target, start, end)

Parameters:

参数 描述
target 必需。复制到指定目标索引位置。
start 可选。元素复制的起始位置。
end 可选。停止复制的索引位置 (默认为 array.length)。如果为负值,表示倒数。

Return value: Returns the copied array

Code example:


//  Copy the first two elements of the array onto the next two elements: 
var fruits = ["Banana", "Orange", "Apple", "Mango"]; 
fruits.copyWithin(2, 0);//Banana,Orange,Banana,Orange

//  Copies the first two elements of the array to the 3 And 4 In a position: 
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.

Syntax:


array.fill(value, start, end)

Parameters:

参数 描述
value 必需。填充的值。
start 可选。开始填充位置。
end 可选。停止填充位置 (默认为 array.length)

Return value: Array

Code example:


// Padding  "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 an array with a fixed value: 
var fruits = ["Banana", "Orange", "Apple", "Mango"]; 
fruits.fill("Runoob");//Runoob,Runoob,Runoob,Runoob

As for the calculation method of index, the two methods are the same

The start index is used to specify the location where to start filling, which is optional. If no ending index is provided, 1 is filled straight to the end of the array. Negative indexes are evaluated from the end of the array. You can also think of a negative index as an array length plus one positive index. Silently ignores index ranges beyond array boundaries, zero length and opposite directions.

Code example:


const zeroes = [0,0,0,0,0];

// Use 6 Fill index greater than or equal to 3 Elements of 
zeroes.fill(6, 3);//[0,0,0,6,6]
zeroes.fill(0);// Reset 

// Use 7 Fill index greater than or equal to 1 And less than 3 Elements of 
zeroes.fill(7,1,3);//[0,7,7,0,0]
zeroes.fill(0);// Reset 

// Use 8 Fill index greater than or equal to 1 And less than 4 Elements of 
//(-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 is too high, ignore 
zeroes.fill(1,10,15);//[0,0,0,0,0]
// Index reverse, ignore 
zeroes.fill(2,4,2);//[0,0,0,0,0]
// The index section is available, and the available section is populated 
zeroes.fill(4,3,10);//[0,0,0,4,4]

Related articles: