JS Array method (ForEach (), every (), reduce ())

Source: Internet
Author: User

Iterator Foreachevery One, iterator method

Iterator methods, which apply a function to each element in an array, and can return a value, a set of values, or a new array

Iterator method that does not generate a new array

1.forEach ()


ForEach ()

Output


foreach Output results

2.every ()
The method uses the function for each element in an array by accepting a function that returns a Boolean value, and if, for all elements, the return value is True, the method returns true, otherwise false


Every ()
When nums = [2,4,6,8,10] is output all numbers is even, nums = [1,2,3,4,5] When output not all ...
The Some () method also accepts a function that returns a Boolean value, but the method returns true whenever an element returns TRUE.

3.reduce ()
The method takes a function that returns a value. The method starts with an accumulated value, continues to use the function for the accumulated value and the elements after it, until the last element of the array, and then returns the accumulated value.


Reduce ()

JS Array method (ForEach (), every (), reduce ())

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.