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 --> 猜你想看 主流博客系统搭建(WordPress、Typecho) - 保姆级教学 vue状态管理详解以及在vue3中使用vuex Vue3 中的 watch 和 watchEffect 详解 Go基本数据类型 vue组件数据共享 jQuery事件 发送网站显示卡片效果 typecho打开没有静态资源文件 Typecho自动生成Sitemap插件 宝塔反向代理,自建CDN节点 最后修改:2022 年 06 月 05 日 © 允许规范转载 赞 0 如果觉得我的文章对你有用,请随意赞赏