Javascript questions
Array methods
The .map() method
The .map() methods iterates over an array, computes whatever logic you write inside the map body, and returns a NEW array. Can be used for chaining other methods because of the return.
The .forEach() method
ForEach is similar to .map() but it DOES NOT return an array. Cannot be used for chaining
The .filter() method
The filter method, as the name suggests, helps in filtering out the values inside of an array based on a Boolean condition.
If the boolean condition is true, the result will be returned and added in the final array. If not, it will be skipped. Filter also returns an array, just like the .map() method. Can be chained
The .reduce() method
In simple terms, the .reduce() method takes into account a previous value , current value and an accumulator.
The return type of the .reduce() method is always a single value. It is useful when you want to process all the values of the array and want to derive some accumulated result.
Here, the currentObj is the object that is being iterated over. Also, the acc value stores the result and is outputted finally into the totalAge array.