Iterative methods for arrays (every, filter, ForEach, map, some)

Source: Internet
Author: User

Every:

Each item in an array runs the given function, and returns TRUE if the function returns true for each item.

var number = [1,2,3,4,5,6]; var result = Number.every (function(item,index,array) {    return (item > 0 )}) Console.log (result)   //true

For every (), the incoming function must return true for each entry, which returns TRUE, otherwise he returns false;

Some

Returns True if the function returns true for any of the given functions in an array.

var number = [1,2,3,4,5,6]; var result = Number.some (function(item,index,array) {    return (item > 5  )}) Console.log (result)      //true
Filter

An array of items that return true for each item in the given function that returns the function.

var number = [1,2,3,4,5,6]; var result = Number.filter (function(item,index,array) {    return (item > 5 )}) Console.log (result)      //[6]]
Map

Runs the given function for each item in the array, returning the arrays that comprise the result of each function call

var number = [1,2,3,4,5,6]; var result = Number.map (function(item,index,array) {    return (item + 5)}) Console.log (Result)      //[6,7,8,9,10,11]
Foreach:

Each item in the array is given a function. This method has no return value

var number = [1,2,3,4,5,6];number.foreach (function(item,index,array) {    * 5)})

Iterative methods for arrays (every, filter, ForEach, map, some)

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.