In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains how to use copyWithin () and fill () to copy and populate ES6 arrays. Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to use copyWithin () and fill () to copy and populate ES6 arrays.
Bulk copy copyWithin ()
The copyWithin () method is used to copy elements from the specified location of the array to another specified location of the array.
CopyWithin () shallowly copies parts of the array in the specified range and then inserts them at the beginning of the specified index.
Grammar
Array.copyWithin (target, start, end)
Parameters:
Parameter description target is required. Copy to the specified target index location. Start is optional. The starting position of the element copy. End is optional. The index location where replication is stopped (default is array.length). If it is negative, it is reciprocal.
Return value: returns the array after replication
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.
Syntax:
Array.fill (value, start, end)
Parameters:
Parameter description value is required. The value of the fill. Start is optional. Start filling the position. End is 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 calculates the index, the two methods are the same
The start index is used to specify where to start populating, and it is optional.
If no ending index is provided, it is populated until the end of the array.
The negative index is calculated from the end of the array. You can also think of a negative index as an array length plus a positive index.
Silently ignores index ranges that are beyond the boundaries of the array, zero length, and opposite directions.
Code example:
Const zeroes = [0JEOPER0ZOPERIMERE0]; / / populate elements with indexes greater than or equal to 3 zeroes.fill (6,3) with 6; / / zeroes.fill (0) with indexes greater than or equal to 3; / / reset / / populate elements with indexes greater than or equal to 1 and less than 3 with 7 zeroes.fill (7LJOLY3); / / [0LJ 7JEOPERO] zeroes.fill (0) / / reset / / populate elements with an index greater than or equal to 1 and less than 4 with 8 / (- 4+zeroes.length=1) (- 1+zeroes.length=4) zeroes.fill (8); / / [0meme8] / / the index is too low, ignoring zeroes.fill (1meme / 10); / / the index is too high and the index is too high, ignoring zeroes.fill (1mem10) / / the index is reversed, ignoring zeroes.fill (2p4); / / the index section is available and the available part zeroes.fill is filled in. / / here, I believe you have a better understanding of how to copy and populate the ES6 array using copyWithin () and fill (). Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.