ES6---array new method

Source: Internet
Author: User
Tags array length

Find:

let Arr=[1,2,234, ' SDF ',-2];arr.find (function (x) {return x<=2;}) //Result: 1, returns the first qualifying X-value Arr.find ( (X,i,arr) {if (X<2) { Console.log (X,i,arr)}) //results: 1 0 [1, 2, 234, "SDF", -2],-2 4 [1, 2, 234, "SDF",-2]   
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
The parameter of find is a callback function, the callback function can receive 3 parameters, value x, so I, array arr, callback function default return value x.

FindIndex:

let Arr=[1,2,234, ' SDF ',-2];arr.findindex (function< Span class= "Hljs-params" > (x) {return x<=2;}) //Result: 0, returns the index of the first qualifying X-value Arr.findindex (function (X,i,arr) {if (x<< Span class= "Hljs-number" >2) {Console.log (X,i,arr)}}) //results: 1 0 [1, 2, 234, "SDF", -2],-2 4 [1, 2 , 234, "SDF",-2]              
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
FindIndex and find are the same, but the index is returned by default.

includes:

let arr=[1,2,234,‘sdf‘,-2];arr.includes(2);// 结果true,返回布尔值arr.includes(20);// 结果:false,返回布尔值arr.includes(2,3)//结果:false,返回布尔值
    • 1
    • 2
    • 3
    • 4
    • 5
The includes function, like string includes, receives the 2 parameter, the item queried, and the starting position of the query.

Keys:

let arr=[1,2,234,‘sdf‘,-2];for(let a of arr.keys()){ console.log(a)}//结果:0,1,2,3,4 遍历了数组arr的索引
    • 1
    • 2
    • 3
    • 4
    • 5
Keys, traversal of array indexes

values:

let arr=[1,2,234,‘sdf‘,-2];for(let a of arr.values()){ console.log(a)}//结果:1,2,234,sdf,-2 遍历了数组arr的值
    • 1
    • 2
    • 3
    • 4
    • 5
Keys, traversal of array items

Entries:

let arr=[‘w‘,‘b‘];for(let a of arr.entries()){ console.log(a)}//结果:[0,w],[1,b]for(let [i,v] of arr.entries()){ console.log(i,v)}//结果:0 w,1 b
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
Entries, the traversal of an array of key-value pairs.

Fill:

let arr=[ ' W ',  ' B '];arr.fill (  ' I ',  ' I '), change the original array Arr.fill ( ' o ', 1)//Result: [ ' I ',  ' o ') change the original array, the second parameter indicates the fill start position new Array (3) .fill ( ' K ') .fill (1,2)//Result: [ ' K ' ,      
    • 1
    • 2
    • 3
    • 4
The Fill method changes the original array, and when the third argument is greater than the array length, the last digit is the end position.

array.of ():

Array.of(‘w‘,‘i‘,‘r‘)//["w", "i", "r"]返回数组Array.of([‘w‘,‘o‘])//[[‘w‘,‘o‘]]返回嵌套数组Array.of(undefined)//[undefined]依然返回数组Array.of()//[]返回一个空数组
    • 1
    • 2
    • 3
    • 4
The Array.of () method always returns an array, with no type, only a number of 0, and an empty array.

Copywithin:

["W","I","R"]. Copywithin (0)//The array does not change at this time ["W","I","R"]. Copywithin (1)//["W","W","I"], array from position1 begins to be overwritten by the original array, onlyItems prior to 10 remains the same ["W","I", "R",  "B"].copywithin (1,2)//[ "W",  " R ", " B ", " B "], index 2 to the last r,b two items replaced to the original array respectively 1 the beginning of the various, when the number is not enough, the end [ "W",  "I",  "R",  ' B '].copywithin (1,2,3"//[ "W",  "R",  "R",  "B"], strong  1 I replace with          
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
The Copywithin method receives three parameters, the beginning of the replaced data, the beginning of the replacement block, the end of the replacement block (not included), and the 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 lengths in the object, therefore the item must have! Array.From ({' 0 ':' W ',' 1 ':' B ', Length:4})["W", "B", undefined, undefined], array after 2 items have no property to assign value, so undefinedarray.From ({' 0 ':' W ',' 1 ':' B ', Length:1})["W"],length is less than the number of keys, sequentially adds an array//////////////////////// ///let divs=document.getelementsbytagname ( "div"); Array. from (divs) //return div element array Array. From ( Wbiokr ') //["W", "B", "I", "O", "K", "R"]array.from ([1,2,3], function (x) {return x+1}) //[2, 3, 4], the second argument is a callback function              
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
Array.from can convert an object with an array of the lenght property to a group, or convert an object such as a string that can be traversed to an array, which receives 2 parameters, transforms the object with a callback function

ES6---array new method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.