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 --> 猜你想看 tcping不是windows默认命令, tcping怎么安装? Vue Router 中全局守卫和组件守卫的使用方式及示例 TS自动运行+Parcel打包 JS网页计算器 PHP+JS随机访问数据库友链 Goland的常用快捷键 Linux搭建我的世界服务器教程 Vue Nuxt3学习-安装 正则表达式 jQuery图片移动 最后修改:2022 年 06 月 05 日 © 允许规范转载 赞 0 如果觉得我的文章对你有用,请随意赞赏