JavaScript arrays about 3

Source: Internet
Author: User

The first two sections say the basics of creating arrays, queue methods, sorting, and some operation methods, and this section is about iterative and merging methods.

    Every () method & Some () method

These two methods run the given function for each item in the array, and then return a Boolean value that can be understood as the difference between everyone and someone, and look at an example:

vararr=[1,2,3,4,5];vareveryresult=arr.every (function (Item,index,arr) {return(item>2); })        //falsevarsomeresult=arr.some (function (Item,index,arr) {return(item>2); })        //true

All two methods execute a function, the first parameter of the function represents each array element of the iteration, the second parameter represents the subscript, and the third parameter represents the array.

   Filter () method

1 var arr=[1,2,3,4,5]2var filterresult=arr.filter (function(item, Index,arr) {3        return (item>2)4 })    //[ 3,4,5]

The method executes the given function for each item in the array, and returns a set of items that the function returns True.

    Map () Method & ForEach () method

Map () returns an array of the results of each function call, and ForEach () does not return a value.

1 vararr=[1,2,3,4,5]2 varMapresult=arr.map (function(Item,index,arr) {3        returnItem*24})//[2,4,6,8,10]5 varForresult=arr.foreach (function(Item,index,arr) {6        //perform some actions7})//undefined

ForEach () is essentially the same as using a for loop to iterate over an algebraic group.

    Reduce () method & Reduceright () method

1 var arr=[1,2,3]; 2 arr.reduce (function(x,y,index,arr) {34       return x+  Y5 },0);    // 6

Both methods have two parameters, the first is a callback function to execute, and the second represents the initial value to be used as the basis for the merge (optional).

   IndexOf () Method & LastIndexOf ()

1 var arr=[1,2,3,4,5]; 2 arr.indexof (1,-1);    // -1 3 arr.lastindexof (1,-1)    //0

Receives two parameters, the first represents the element to find, the second represents the start position, and if any, returns the position of the element in the array subscript, No 1

The array is probably summed up so much, at the same time the array is also the object, will later in the object's learning summary to explore.

JavaScript arrays about 3

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.