数组的运用和方法的运用

## 对 reduce 等基础的数组方法无法熟练组合运用

const movements = [200, 450, -400, 3000, -650, -130, 70, 1300];
const labelSumIn = document.querySelector('.summary**value--in');
const labelSumOut = document.querySelector('.summary**value--out');
const labelSumInterest = document.querySelector('.summary\_\_value--interest');

const calDisplaySummary = function (movements) {
// 复盘:写代码之前药想清楚通过什么方法?达成什么?
const incomes = movements
.filter(mov => mov > 0)
.reduce((acc, mov) => acc + mov, 0);
labelSumIn.textContent = `${incomes}$`;

const outcomes = movements
.filter(mov => mov < 0)
.reduce((acc, mov) => Math.abs(acc + mov), 0);
labelSumOut.textContent = `${outcomes}$`;
};
calDisplaySummary(account1.movements);


## 数组的灵活使用非常重要

- 使用数组方法带来的巨大优势,而不是简单的使用传统的循环
- 不应该过度使用链接,应该尝试优化他
- 当有非常大的数组,一个接一个的链接大量的方法可能会导致真实的性能问题
- 请不断寻找保持代码性能的机会
- 在 js 中,链接改变底层原始数组的方法是一种糟糕的做法 --> 其中之一就是拼接法
->!!! 后续自己思考,特别是关于从数据库接收到的值,在页面产生变化时,如何往后端传值。不应该链接像 splice 或 reverse 方法这样的方法
posted @ 2024-01-18 20:49  continentliang  阅读(3)  评论(0编辑  收藏  举报