//For each
const array = [1, 2, 10, 16];
const double = [];
const newArray = array.forEach(
(num) => {
double.push(num * 2);
}
)
//For EACH ONLY WANNTS TO ITERATE AND APPLY WHATEVER WE WANT
//With for each u can do a lot of SIDE EFFECTS which is not that good
//map , filter, reduce = the most important methods ====================
//map is like a PURE FUNCTION WHICH IS GOOD
//same as above, but RETURN is a must, automatically returns new array
const mapArray = array.map((num) => {
return num * 2
})
//Since we use only one parametr and one single like, we can write it like this:
const mapArray = array.map(num => num * 2);
//MAP WANTS TO ITERATE AND RETURN, MAP CREATES A NEW ARRAY
//Filter ==============================
//filter array with a condition
//we also RETURN an array
const filterArray = array.filter((num) => {
return num > 5
})
//or like this
const filterArray = array.filter(num => num > 5);
//Reduce =====================================
//VERY POWERFUL!!!!
const reduceArray = array.reduce((accumulator, num) => {
debugger;
return accumulator + num
}, 5);
//Last 5 is an accumulator.
//a number that is stored through the process return is not an array