Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use reduce in js array

Shulou Source: shulou.com Published: 2022-06-02 23:44:34 10月13日 Update

This article mainly introduces how to use reduce in the js array, which is very detailed and has a certain reference value. Interested friends must read it!

1. The reduce () method executes a reducer function on each element in the array and summarizes the results into a single return value.

2. It takes two parameters, a reducer function that provides each call to the array, and an optional initial value parameter.

Example

Find the sum of array items

Var sum = arr.reduce (function (prev, cur) {return prev + cur;}, 0)

Find the maximum value of the array item

Var max = arr.reduce (function (prev,cur) {return Math.max (prev,cur);}); these are all the contents of the article "how to use reduce in js arrays". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Array content function parameter article maximum sum of two value element interest single instance partner method more maximum knowledge result Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MySQL NVidia MariaDB macOS