Reducing Arrays
Javascript
The 'reduce' method iterates through an array and applies a function to each element, accumulating the result into a single value. For example, `[1, 2, 3, 4].reduce((sum, num) => sum + num, 0);` will result in `10` (the sum of all elements). The initial value (`0`) is the starting point for the accumulator.
Array Sorting
Javascript
The 'sort' method modifies the original array in place. By default, it sorts elements alphabetically or numerically. To sort in ascending order, you can use a custom compare function. For example, `[3, 1, 4, 2].sort((a, b) => a - b);` results in `[1, 2, 3, 4]`.
Iterating through Arrays
Javascript
The 'forEach' method allows you to execute a provided function once for each element in an array. For example, `['apple', 'banana', 'orange'].forEach(fruit => console.log(fruit));` will output each fruit to the console: 'apple', 'banana', 'orange'.
Filtering Arrays
Javascript
The 'filter' method creates a new array containing only elements that meet a specified condition. You can use a callback function to define this condition. For example, `const evenNumbers = [1, 2, 3, 4, 5].filter(num => num % 2 === 0);` results in `evenNumbers = [2, 4]`.
Array methods
Javascript
The 'map' method allows you to iterate through each element of an array and apply a function to it. In this case, we create a new array where each element is doubled. For example, `const doubledArray = [1, 2, 3, 4, 5].map(num => num * 2);` will result in `doubledArray = [2, 4, 6, 8, 10]`.