Array Functios Flashcards
1
Q
What are the benefits of using array methods (forEach, map, etc.) over loops?
A
- they’re more meaningful and expressive
- the code is more declarative – it means that I define what I want to reach instead of describing the way how to do it
- using loops we need to care about the number of elements, starting point, and the iteration process
- A programming language is low level when its programs require attention to the irrelevant.” — Alan Perlis
2
Q
How does .map() work?
A
- .map is applied to an array
- It accepts one parameter: a transformation function
- It returns a new array containing the same number of elements as the original array but with the transformation applied
- The original array is not modified
- We always receive a new array, so we need to assign the result to a variable
const strings = ['42', '23', '15', '2']; const numbers = strings.map(s => Number(s)); // [42, 23, 15, 2] (notice the lack of quote ')
3
Q
How does .filter() work?
A
- The filter function is applied to an array
- It accepts one parameter: a condition function (also called a predicate or callback function)
- It returns a new array with only the elements for which the condition is true
- It always returns a new array, and the original array is not modified
- Filter can be used on arrays with elements of any type
const numbers = [4, 2, 1, 3, 5, 8]; const evens = numbers.filter(num => num % 2 === 0); // [4, 2, 8]
- The following example does not assign the result, so it is not used and is ignored.
numbers. filter(num => num % 2 === 0);
4
Q
How does .reduce() work?
A
- reduce accepts two parameters: a reducer function and an initial value
- It is named ‘reduce’ because it reduces a collection of elements to a single value by applying the reducer function to each element
- The initial value is used in the first iteration, as the accumulated value
- Iterating the array, at each step, the reducer function is called
- The reducer function receives the accumulated value and the current element of the collection and combines them in some way
- Any sort of collection can be reduced
- If you don’t supply an initial value, the first element of the collection will be used
- Reduce can return any type of data - array, boolean, string, etc.
const numbers = [2,5,7]; const sum = numbers.reduce((sum, number) => sum + number, 0);
const animals = ['cat', 'dog']; const joined = animals.reduce((res, animal) => res + ', ' + animal)); // no initial value
const joined = animals.reduce((res, animal) => res + ‘, ‘ + animal, ‘’); // with initial value
5
Q
How can implement filter by using reduce?
A
const filter = (array = [], filterFn) => { const result = array.reduce((acc, el, index, arr) => { if (filterFn(el, index, arr)) { acc.push(el); } return acc; }, []);
return result;
};
const filtered = filter([2, 6, 5, 8, 10], (el) => el > 5); console.log(filtered); // [ 6, 8, 10 ]
6
Q
How can implement mapby using reduce?
A
const map = (array = [], mapFn) => { const result = array.reduce((acc, el, index, arr) => { acc.push(mapFn(el, index, arr)); return acc; }, []);
return result;
};
console.log(map([1, 2, 3], (el) => el * 2)); // [ 2, 4, 6 ]