Methods that returns new Array:
Methods that mutates Array:
Methods that perform actions:
map()
The `map()` method is probably the most common when it comes to React. It is used for creating what are called "lists", which are essentially arrays of elements. These elements could be React components or HTML tags with content.
The `map()` method creates a new array with the results of calling a provided function on every element in the calling array.
So the function we passed into the `map()` method is called for each element in the `notes` array. The `note` parameter is the current element being processed. The function returns the `title` property of the `note` object, which is then added to the `noteTitles` array.
filter()
The `filter()` method creates a new array with all elements that pass the test implemented by the provided function. So it is similar to the `map()` method, but instead of transforming the elements, it filters them based on a condition.
In React, this method is often used for deleting or removing elements from the UI.
reduce()
The `reduce()` method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.
This is useful for summing up numbers, concatenating strings, or any other operation that requires combining elements of an array.
The function we pass in takes in two parameters: the `total` accumulator and the current `number` being processed.
total (hoặc các tên tương tự): là số tổng hay kết hợp sẽ được cộng dồn number (hoặc các tên tượng tự): là giá trị hiện tại của Array đang chạy 0 (hoặc bất cứ giá trị nào truyền vào): là giá trị khởi đầu của cộng dồn The function returns the sum of the `total` and `number`, which is then passed to the next iteration. The `0` at the end is the initial value of the `total` accumulator.
forEach()
The `forEach()` method executes a provided function once for each array element.
It is similar to the `map()` method, but it does not return a new array.
It is useful when you want to perform an action on each element in the array.
notes.forEach((note) => console.log(note.title));