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 --> 猜你想看 Vue Router 多个路由的概念和实现方式详解 Typecho自动生成Sitemap插件 go语言结构体 宝塔7.8版本降级7.7+插件 每日一学:PHP 中的array_shift函数详解 handsome美化-持续更新 react学习-JSX(二) 前端开发快速删掉 node_modules 小工具 每日一学:PHP 中的array_replace函数详解 每日一学:PHP 中的array_combine函数详解 最后修改:2022 年 06 月 05 日 © 允许规范转载 赞 0 如果觉得我的文章对你有用,请随意赞赏