Summary of the New Method Instance of array in ES6, es6array
This example describes how to add an array in es6. We will share this with you for your reference. The details are as follows:
● Find:
Let arr = [234, 'sdf ',-2]; arr. find (function (x) {return x <= 2 ;}) // result: 1, returns the first x value arr that meets the condition. find (function (x, I, arr) {if (x <2) {console. log (x, I, arr) }}) // result: 1 0 [1, 2,234, "sdf",-2],-2 4 [1, 2,234, "sdf",-2]
The find parameter is the callback function. The callback function can receive three parameters, value x, so I, array arr. The callback function returns x by default.
● FindIndex:
Let arr = [234, 'sdf ',-2]; arr. findIndex (function (x) {return x <= 2 ;}) // result: 0, returns the arr of the first x value that meets the condition. findIndex (function (x, I, arr) {if (x <2) {console. log (x, I, arr) }}) // result: 1 0 [1, 2,234, "sdf",-2],-2 4 [1, 2,234, "sdf",-2]
FindIndex is similar to find, but the index is returned by default.
● Des:
Let arr = [234, 'sdf ',-2]; arr. except des (2); // returns true, returns a Boolean value of arr. except des (20); // result: false, return a Boolean value of arr. except des (2, 3) // result: false. A boolean value is returned.
The except des function is the same as the string's except des function. It receives two parameters, query items, and query start position.
● Keys:
Let arr = [234, 'sdf ',-2]; for (let a of arr. keys () {console. log (a)} // result: the indexes of array arr are traversed by, and 4.
Keys: traversal of array Indexes
● Values:
Let arr = [234, 'sdf ',-2]; for (let a of arr. values () {console. log (a)} // result: 1, 2, 234, sdf,-2 traverses the array arr Value
Keys: traversal of array items
● Entries:
Let arr = ['w', 'B']; for (let a of arr. entries () {console. log (a)} // result: [0, w], [1, B] for (let [I, v] of arr. entries () {console. log (I, v)} // result: 0 w, 1 B
Entries: traversal of array key-value pairs.
● Fill:
Let arr = ['w', 'B']; arr. fill ('I') // result: ['I', 'I'], change the original array arr. fill ('O', 1) // result: ['I', 'O'] changes the original Array. The second parameter indicates the starting position of filling new Array (3 ). fill ('k '). fill ('R', 1, 2) // result: ['k', 'R', 'K']. The third array indicates the end position of the filling.
The fill method changes the original array. When the third parameter is greater than the array length, the last bit is the ending position.
● Array. ():
Array. of ('w', 'I', 'R') // ["w", "I", "r"] returns an Array. of (['w', 'O']) // [['w', 'O'] returns the nested Array. of (undefined) // [undefined] returns an Array. of () // [] returns an empty array
The Array. of () method always returns an Array. parameters are of no type and only count is allowed. If the number is 0, an empty Array is returned.
● CopyWithin:
["W", "I", "r"]. copyWithin (0) // at this time, the array does not change ["w", "I", "r"]. copyWithin (1) // ["w", "w", "I"], the array is overwritten by the original array from position 1, only item 0 before 1 remains unchanged ["w", "I", "r", "B"]. copyWithin (1, 2) // ["w", "r", "B", "B"], index 2 to the final r, the two items B are replaced with the items starting from the original array 1. If the number is not enough, change to ["w", "I", "r", 'B']. copyWithin (1st, 3) // ["w", "r", "r", "B"], replace the I of the top 2nd items with the r of the top items
The copyWithin method receives three parameters: Start of the replaced data, start of the replaced data block, end of the replaced data block (not included), and copyWithin (s, m, n ).
● Array. from ():
Array. from ({'0': 'w', '1': 'B', length: 2}) // ["w", "B"], the length of the returned array depends on the length of the object! Array. from ({'0': 'w', '1': 'B', length: 4}) // ["w", "B", undefined, undefined], no attribute is assigned for the last two items of the array, so undefinedArray. from ({'0': 'w', '1': 'B', length: 1}) // ["w"], length is less than the number of keys, add arrays in sequence
Let divs = document. getElementsByTagName ('div '); Array. from (divs) // returns the Array of div elements. from ('wbiokr') // ["w", "B", "I", "o", "k", "r"] Array. from ([1, 2, 3], function (x) {return x + 1}) // [2, 3, 4], the second parameter is the callback function
Array. from can convert objects with properties similar to arrays into arrays, or convert objects such as strings that can be traversed into arrays. It receives two parameters and converts objects and callback functions.
For more information, see this topic: ECMAScript6 (ES6) getting started, JavaScript Array Operation Skills summary, JavaScript character and string operation skills summary, JavaScript data structure and algorithm skills summary, JavaScript errors and debugging skills Summary and javascript object-oriented tutorials
I hope this article will help you with ECMAScript-based programming.