Loops and iterations of JavaScript arrays

Source: Internet
Author: User

Loops and iterations of JavaScript arrays

Cycle

For loop

Normal version for loop

var arr = ["a","b","C", " D " ];  for 0; i < arr.length; i++) {      console.log (arr[i]);    . // a b c d}

Optimized for Loop

var arr = ["a","b","C", " D " ];  for 0; Len =  Arr.length;i < len;  i++) {      console.log (arr[i]);    . // a b c d}

For In loop

var arr = ["a","b","C", " D " ];  for inch arr) {      console.log (arr[i]);    . // a b c d}

For of Loops

var arr = ["a","b","C", " D " ];  for (Let item of arr) {      console.log (item);    . // a b c d}

Description

    1. For-in is the key name that iterates through the collection object, and for-of is the key value that traverses the collection object directly.
    2. For-of has a better traversal performance than for-in, but it is still more than normal for loop.
    3. For-of not only supports array traversal, but also supports most class array objects. also supports new MAP and Set objects in ES6

Array iterations

ForEach ()

Description: Sequentially enables each item in the array to perform some of the same operations sequentially.

var arr = [ten,];arr.foreach (The function (item, Index ) {      Console.log (item);            //       console.log (index);         // 0 1 2) 3    4})    

Description

    1. The array comes with a ForEach loop that uses a higher frequency but is actually weaker than the normal for loop.

ForEach cannot use the continue and break statement to interrupt a loop, nor can it return to an outer function using a return statement sentence.

Map ()

Description: Sequentially let each item in the array perform some of the same operation sequentially, and then return a new array

var arr = [tento one]; var newArr = arr.map (function (item, index) {      return2;})    Console.log (NEWARR);            // [26,28]

Filter ()

Description: Sequentially let each item in the array perform some of the same operations sequentially, then filter out the elements that match the condition, make up a new array, and return.

var arr = [tento one]; var newArr = arr.filter (function (item, index) {      return20 ; })    Console.log (NEWARR);            // [Ten, A.]

Feduce ()

Description: Make a calculation of the preceding and latter in the array sequentially, and return the final value cumulatively.

var arr = [tento one]; var result = arr.reduce (function (prev, next) {      return prev + Next;})    Console.log (result);            //    -

Every ()

Description: Each item in the array is sequentially followed by a similar operation to detect whether each item in the array meets the criteria.

var arr = [tento one]; var result = arr.every (function (item, index) {      return20  ; })    Console.log (result);            //   false

Description: A detected element that returns true if all conditions are met, false if one of the items does not meet the criteria. That is, a false must leave.

Some ()

Description: Each item in the array is sequentially executed in one of the same actions to detect whether an item in the array is eligible.

var arr = [tento one]; var result = arr.some (function (item, index) {      return20;})    Console.log (result);            //   True

Description: In the instrumented element, if one of the items matches the condition, the result is true and the result is false if none of the conditions are met. That one will be true.

Loops and iterations of JavaScript arrays

Related Article

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.