Loading... <!-- wp:heading --> <h2 id="前言">前言</h2> <!-- /wp:heading --> <!-- wp:paragraph --> <p>此函数是我在写Vue购物车小案例时写的的,因为开发中经常使用到reduce,所以我便给他封装成了一个函数</p> <!-- /wp:paragraph --> <!-- wp:heading --> <h2 id="使用说明">使用说明</h2> <!-- /wp:heading --> <!-- wp:code --> <pre class="wp-block-code"><code>function arrSum(arr) { if (typeof arr != 'object') { return '数据类型错误!' } return arr.reduce((total, item, index) => { if (typeof item != 'number') return '第' + index + '位错误! Bit ' + index + ' data error!' return total + item }, 0) } </code></pre> <!-- /wp:code --> <!-- wp:paragraph --> <p>使用时向arrSum()传入一个数组</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>例子:</p> <!-- /wp:paragraph --> <!-- wp:code --> <pre class="wp-block-code"><code>const num = [1, 3, 5, 8, 10] arrSum(num) </code></pre> <!-- /wp:code --> 猜你想看 vue3新特性 每日一学:PHP 中的array_merge函数详解 Vue Router 的多个路由定义和使用方法详解 react学习-组件和事件绑定(三) JS使用reduce求和 今天吃什么-解决选择困难症 酷番云云服务器优惠活动全解析:2025年超值上云特惠方案 Redis学习之安装Redis Golang语言优势和其他语言的对比 vue组件数据共享 最后修改:2022 年 06 月 05 日 © 允许规范转载 赞 0 如果觉得我的文章对你有用,请随意赞赏