Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use reduce and reduceRight methods for array iteration

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian for you to introduce in detail the "array iterative reduce and reduceRight method how to use", the content is detailed, the steps are clear, the details are handled properly, I hope that this "array iterative reduce and reduceRight method how to use" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

Array.reduce ()

The reduce () method runs the function on each array element to generate (decrease it) a single value.

The reduce () method works from left to right in the array. See also reduceRight ().

The reduce () method does not reduce the original array.

This example determines the sum of all the numbers in the array:

Example

Var numbers1 = [45, 4, 9, 16, 25]

Var sum = numbers1.reduce (myFunction)

Function myFunction (total, value, index, array) {

Return total + value

}

Note that this function accepts four parameters:

Total (initial value / previously returned value)

Project value

Project index

The array itself

The index and array parameters are not used in the above example. You can rewrite it as:

Example

Var numbers1 = [45, 4, 9, 16, 25]

Var sum = numbers1.reduce (myFunction)

Function myFunction (total, value) {

Return total + value

}

The reduce () method can accept an initial value:

Example

Var numbers1 = [45, 4, 9, 16, 25]

Var sum = numbers1.reduce (myFunction, 100)

Function myFunction (total, value) {

Return total + value

}

All browsers support Array.reduce (), except for Internet Explorer 8 or earlier:

Array.reduceRight ()

The reduceRight () method runs the function on each array element to generate (decrease it) a single value.

The reduceRight () method works from right to left in the array. See also reduce ().

The reduceRight () method does not reduce the original array.

This example determines the sum of all the numbers in the array:

Example

Var numbers1 = [45, 4, 9, 16, 25]

Var sum = numbers1.reduceRight (myFunction)

Function myFunction (total, value, index, array) {

Return total + value

}

Note that this function accepts four parameters:

Total (initial value / previously returned value)

Project value

Project index

The array itself

The index and array parameters are not used in the above example. You can rewrite it as:

Example

Var numbers1 = [45, 4, 9, 16, 25]

Var sum = numbers1.reduceRight (myFunction)

Function myFunction (total, value) {

Return total + value

}

All browsers support Array.reduceRight (), except for Internet Explorer 8 or earlier

After reading this, the article "how to use array iterative reduce and reduceRight methods" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report