Home  >  Article  >  Web Front-end  >  Detailed explanation of Javascript reduce function duplicate checking

Detailed explanation of Javascript reduce function duplicate checking

黄舟
黄舟Original
2017-03-23 14:23:142085browse

The reduce function is an array method that appears in the ECMAScript5 specification. Let me share with you through this article JavascriptThe classic interview routine reduce function check for duplication, friends in need can refer to it

Today in I came across a piece of code by chance. The code used a very short length to complete the classic interview question of counting the number of identical characters in a string. The reduce method was used. I checked online and found nothing valuable, resulting in waste. It took me some time to understand, and now I organize my ideas as follows:

Original code:

var arr="qweqrq"
var info= arr.split('').reduce((a,b)=>
 (a[b]++ || (a[b]=1),a)
,{})
console.log(info)

The code idea is like this, first cut the string arr into an array through the split method, Then use reduce. What does this method do? It is divided into these steps:

1. First, reduce will receive a callback to execute each element in the array. If there is a second parameter, as in the above example: {}. , then the callback will use this {} as a parameter to pass it into the callback together with the first element in the array;

2. After passing in the parameters, an AND gate short-circuit operation will be performed, which can also be called default Value operation, when a[b]++ becomes true, return a[b]. Here a is {} and b is "q". Obviously there is no b in a. If a[b]=1 is executed, a[b] will be assigned a value of 1, followed by a comma expression. Formula , so it will return an a object with a[b]:1 such attribute;

AND gate short-circuit operation: if the first operand The value is true, a short-circuit operation is performed, and the value of the first operand is directly generated. If false, yields the value of the second operand.

Comma expression: The general form of comma expression is: expression 1, expression 2, expression 3... expression n. The solution process of the comma expression is: first calculate the value of expression 1, then calculate the value of expression 2,... until the value of expression n is calculated. Finally, the value of the entire comma expression is the value of expression n.

3. Let’s talk about the reduce function later. It can use the result returned after the previous execution as a parameter and continue to pass it into the callback for execution together with the subsequent elements, which is equivalent to fn(fn(fn(fn(a, b), c), d), e), fn is the callback;

4. In this example, because the second parameter {} is passed in, the callback is executed 6 times. It is equivalent to entering and checking duplicates for each element in the array. For example, when the callback is run for the fourth time, a is {q:1,w:1,e:1}, and the callback is executed, a[ b] That is, a['q'] exists. If a['q']++ is executed, the value of a['q'] is 2, and so on. The final result is an attribute name with each element of the array, which appears. An object whose times are attribute values.

It should be noted that reduce is a method introduced by es5 and is not compatible with ie8 and below.

The above is the detailed content of Detailed explanation of Javascript reduce function duplicate checking. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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