-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreduce.js
More file actions
21 lines (19 loc) · 796 Bytes
/
reduce.js
File metadata and controls
21 lines (19 loc) · 796 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* -------------------------------------------------------------------------- */
/* $Reduce */
/* ---------------------- Para acumular muchos valores ---------------------- */
/* -------------------------------------------------------------------------- */
const carrito = [
{ name: "tv", precio: 500 },
{ name: "laptop", precio: 300 },
{ name: "celular", precio: 200 },
];
// foreach
let totalFE = 0;
carrito.forEach((element) => (totalFE += element.precio));
console.log(totalFE); /* 1000 */
// reduce: el cero es el valor inicial en el q empieza el acumulador al final se retorna el acumulador
let total = carrito.reduce(
(acumulador, producto) => acumulador + producto.precio,
0
);
console.log(total); /* 1000 */