Home > Web Front-end > JS Tutorial > How to use reduceRight function

How to use reduceRight function

不言
Release: 2019-02-21 13:29:26
Original
2515 people have browsed it

array.reduceright() is a built-in function in javascript that converts the elements of a given array from right to left into a single value. So let's take a look at the specific use of the reduceRight function.

How to use reduceRight function

Let’s take a look at the basic syntax of the reduceRight function

array.reduceRight(previousValue, currentValue)
Copy after login

The two parameters previousValue and currentValue represent the previous sum of the given input array The current element.

Let’s look at two specific examples

Example 1:

The code is as follows

<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>
<script> 
  
const A = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8 ] ]; 
  
a = A.reduceRight((previousValue, currentValue) => previousValue.concat(currentValue)); 

document.write(a); 
  
</script> 
</body>
</html>
Copy after login

The output result is: 7,8,4,5 ,6,1,2,3

Example 2:

The code is as follows

<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>
<script> 
const A = [ [ 1, 2, 3 ], [ "a", "b", "c" ], [ 7, 8 ] ]; 
a = A.reduceRight((previousValue, currentValue) => previousValue.concat(currentValue)); 
document.write(a); 
  
</script>
</body>
</html>
Copy after login

The output result is: 7,8,a,b,c,1,2, 3

This article has ended here. For more exciting content, you can pay attention to other related column tutorials on the php Chinese website! ! !

The above is the detailed content of How to use reduceRight function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template