Array of methods Some,every, reduce simple method

Source: Internet
Author: User

The 1.some method sequentially detects whether each element in the array conforms to the condition of a given function, returns a Boolean value, does not process an empty array, and does not alter the original array. In execution, there is a satisfaction that returns true and no longer continues execution

var aa = [1,32,4,26];var bb = aa.some(function(item){    return item > 30;})console.log(bb); // 输出为true
Attention issues

The some callback function has three parameters, one is the current element (must), one is the index of the current element (optional), and one is the array object that the current element belongs to.

The 2.every method sequentially detects whether each element in the array conforms to the condition of a given function, returns a Boolean value, does not process an empty array, and does not alter the original array. All elements are satisfied before returning true

var aa = [3,32,4,26];var bb = aa.every(function(item){    return item > 2;})console.log(bb); // 输出为true
Attention issues

The every callback function has three parameters, one is the current element (must), one is the index of the current element (optional), and one is the array object that the current element belongs to.

The 3.reduce method returns the final value for each element in the array, in turn, by the method of the callback function.

var aa = [3,2,4,1];var bb = aa.reduce(function(total,item){    return total+item;})console.log(bb); // 输出为10
Attention issues

The reduce callback function has four parameters, the first is the sum (must), the value returned, the second is the current element (must), and the third is the index of the current element (optional), and one is the array object to which the current element belongs.

Array of methods Some,every, reduce simple 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.