企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# vue---reduce高级与filter去重 > reduce()方法可以搞定的东西,for循环,或者[forEach](https://so.csdn.net/so/search?q=forEach)方法有时候也可以搞定,那为啥要用reduce()?这个问题,之前我也想过,要说原因还真找不到,唯一能找到的是:通往成功的道路有很多,但是总有一条路是最捷径的,亦或许reduce()逼格更高... arr.reduce(callback,\[initialValue\]) 参数是 回调函数 和初始值 callback (执行数组中每个值的函数,包含四个参数) ``` <pre class="calibre13">``` <span class="token5">1</span>、previousValue (上一次调用回调返回的值,或者是提供的初始值(initialValue)) <span class="token5">2</span>、currentValue (数组中当前被处理的元素) <span class="token5">3</span>、index (当前元素在数组中的索引) <span class="token5">4</span>、array (调用 reduce 的数组) ``` ``` initialValue (作为第一次调用 callback 的第一个参数。) 2、实例解析 initialValue 参数 先看第一个例子: ``` <pre class="calibre13">``` <span class="token2">var</span> arr <span class="token">=</span> <span class="token1">[</span><span class="token5">1</span><span class="token1">,</span> <span class="token5">2</span><span class="token1">,</span> <span class="token5">3</span><span class="token1">,</span> <span class="token5">4</span><span class="token1">]</span><span class="token1">;</span> <span class="token2">var</span> sum <span class="token">=</span> arr<span class="token1">.</span><span class="token3">reduce</span><span class="token1">(</span><span class="token2">function</span><span class="token1">(</span>prev<span class="token1">,</span> cur<span class="token1">,</span> index<span class="token1">,</span> arr<span class="token1">)</span> <span class="token1">{</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>prev<span class="token1">,</span> cur<span class="token1">,</span> index<span class="token1">)</span><span class="token1">;</span> <span class="token2">return</span> prev <span class="token">+</span> cur<span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span> ``` ``` console.log(arr, sum); 打印结果: ``` <pre class="calibre13">``` <span class="token5">1</span> <span class="token5">2</span> <span class="token5">1</span> <span class="token5">3</span> <span class="token5">3</span> <span class="token5">2</span> <span class="token5">6</span> <span class="token5">4</span> <span class="token5">3</span> \<span class="token1">[</span><span class="token5">1</span><span class="token1">,</span> <span class="token5">2</span><span class="token1">,</span> <span class="token5">3</span><span class="token1">,</span> <span class="token5">4</span>\<span class="token1">]</span> <span class="token5">10</span> ``` ``` 这里可以看出,上面的例子index是从1开始的,第一次的prev的值是数组的第一个值。数组长度是4,但是reduce函数循环3次。 例子二: ``` <pre class="calibre13">``` <span class="token2">var</span> arr <span class="token">=</span> <span class="token1">[</span><span class="token5">1</span><span class="token1">,</span> <span class="token5">2</span><span class="token1">,</span> <span class="token5">3</span><span class="token1">,</span> <span class="token5">4</span><span class="token1">]</span><span class="token1">;</span> <span class="token2">var</span> sum <span class="token">=</span> arr<span class="token1">.</span><span class="token3">reduce</span><span class="token1">(</span><span class="token2">function</span><span class="token1">(</span>prev<span class="token1">,</span> cur<span class="token1">,</span> index<span class="token1">,</span> arr<span class="token1">)</span> <span class="token1">{</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>prev<span class="token1">,</span> cur<span class="token1">,</span> index<span class="token1">)</span><span class="token1">;</span> <span class="token2">return</span> prev <span class="token">+</span> cur<span class="token1">;</span> <span class="token1">}</span>,<span class="token5">0</span><span class="token1">)</span> <span class="token6">//注意这里设置了初始值</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>arr<span class="token1">,</span> sum<span class="token1">)</span><span class="token1">;</span> ``` ``` ``` <pre class="calibre13">``` 打印结果: <span class="token5">0</span> <span class="token5">1</span> <span class="token5">0</span> <span class="token5">1</span> <span class="token5">2</span> <span class="token5">1</span> <span class="token5">3</span> <span class="token5">3</span> <span class="token5">2</span> <span class="token5">6</span> <span class="token5">4</span> <span class="token5">3</span> \<span class="token1">[</span><span class="token5">1</span><span class="token1">,</span> <span class="token5">2</span><span class="token1">,</span> <span class="token5">3</span><span class="token1">,</span> <span class="token5">4</span>\<span class="token1">]</span> <span class="token5">10</span> ``` ``` 这个例子index是从0开始的,第一次的prev的值是我们设置的初始值0,数组长度是4,reduce函数循环4次。 结论:`如果没有提供initialValue,reduce 会从索引1的地方开始执行 callback 方法,跳过第一个索引。如果提供initialValue,从索引0开始。` 注意:如果这个数组为空,运用reduce是什么情况? var arr = \[\]; var sum = arr.reduce(function(prev, cur, index, arr) { console.log(prev, cur, index); return prev + cur; }) //报错,"TypeError: Reduce of empty array with no initial value" 但是要是我们设置了初始值就不会报错,如下: var arr = \[\]; var sum = arr.reduce(function(prev, cur, index, arr) { console.log(prev, cur, index); return prev + cur; },0) console.log(arr, sum); // \[\] 0 ## 简单用法 ``` <pre class="calibre13">``` var arr <span class="token">=</span> <span class="token1">[</span><span class="token5">1</span><span class="token1">,</span> <span class="token5">2</span><span class="token1">,</span> <span class="token5">3</span><span class="token1">,</span> <span class="token5">4</span><span class="token1">]</span><span class="token1">;</span> var sum <span class="token">=</span> arr<span class="token1">.</span><span class="token3">reduce</span><span class="token1">(</span><span class="token1">(</span>x<span class="token1">,</span>y<span class="token1">)</span><span class="token">=</span><span class="token">></span>x<span class="token">+</span>y<span class="token1">)</span> var mul <span class="token">=</span> arr<span class="token1">.</span><span class="token3">reduce</span><span class="token1">(</span><span class="token1">(</span>x<span class="token1">,</span>y<span class="token1">)</span><span class="token">=</span><span class="token">></span>x<span class="token">*</span>y<span class="token1">)</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span> sum <span class="token1">)</span><span class="token1">;</span> <span class="token6">//求和,10</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span> mul <span class="token1">)</span><span class="token1">;</span> <span class="token6">//求乘积,24</span> ``` ``` ``` <pre class="calibre13">``` let names <span class="token">=</span> <span class="token1">[</span><span class="token4">'Alice'</span><span class="token1">,</span> <span class="token4">'Bob'</span><span class="token1">,</span> <span class="token4">'Tiff'</span><span class="token1">,</span> <span class="token4">'Bruce'</span><span class="token1">,</span> <span class="token4">'Alice'</span><span class="token1">]</span><span class="token1">;</span> let nameNum <span class="token">=</span> names<span class="token1">.</span><span class="token3">reduce</span><span class="token1">(</span><span class="token1">(</span>pre<span class="token1">,</span>cur<span class="token1">)</span><span class="token">=</span><span class="token">></span><span class="token1">{</span> <span class="token2">if</span><span class="token1">(</span>cur <span class="token2">in</span> pre<span class="token1">)</span><span class="token1">{</span> pre<span class="token1">[</span>cur<span class="token1">]</span><span class="token">++</span> <span class="token1">}</span><span class="token2">else</span><span class="token1">{</span> pre<span class="token1">[</span>cur<span class="token1">]</span> <span class="token">=</span> <span class="token5">1</span> <span class="token1">}</span> <span class="token2">return</span> pre <span class="token1">}</span><span class="token1">,</span><span class="token1">{</span><span class="token1">}</span><span class="token1">)</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>nameNum<span class="token1">)</span><span class="token1">;</span> <span class="token6">//{Alice: 2, Bob: 1, Tiff: 1, Bruce: 1}</span> ``` ``` ``` <pre class="calibre14">``` let arr <span class="token">=</span> <span class="token1">[</span><span class="token5">1</span><span class="token1">,</span><span class="token5">2</span><span class="token1">,</span><span class="token5">3</span><span class="token1">,</span><span class="token5">4</span><span class="token1">,</span><span class="token5">4</span><span class="token1">,</span><span class="token5">1</span><span class="token1">]</span> let newArr <span class="token">=</span> arr<span class="token1">.</span><span class="token3">reduce</span><span class="token1">(</span><span class="token1">(</span>pre<span class="token1">,</span>cur<span class="token1">)</span><span class="token">=</span><span class="token">></span><span class="token1">{</span> <span class="token2">if</span><span class="token1">(</span><span class="token">!</span>pre<span class="token1">.</span><span class="token3">includes</span><span class="token1">(</span>cur<span class="token1">)</span><span class="token1">)</span><span class="token1">{</span> <span class="token2">return</span> pre<span class="token1">.</span><span class="token3">concat</span><span class="token1">(</span>cur<span class="token1">)</span> <span class="token1">}</span><span class="token2">else</span><span class="token1">{</span> <span class="token2">return</span> pre <span class="token1">}</span> <span class="token1">}</span><span class="token1">,</span><span class="token1">[</span><span class="token1">]</span><span class="token1">)</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>newArr<span class="token1">)</span><span class="token1">;</span><span class="token6">// [1, 2, 3, 4]</span> ``` ```