The array below will be used to demonstrate all the array methods.
The filter() method creates a new array containing only the items that meets the filter criteria,
like in the example below, al the items where the price is less or equal to 100.
The map() method will iterate over an array, perform some operations on the data and create a new array from that results.
The find() method returns the value of the first element in the provided array that satisfies the provided testing function.
- If you need the index of the found element in the array, use findIndex().
- If you need to find the index of a value, use Array.prototype.indexOf(). (It’s similar to findIndex(), but checks each element for equality with the value instead of using a testing function.)
- If you need to find if a value exists in an array, use Array.prototype.includes().
The foreach() method iterates over ever element in the array, once.
Unlike map(), foreach() will mutate the array if you change the orginal values in the array.
The some() method returns true of false if one of the items in the array matches condition
The every() method returns true of false if all of the items in the array matches condition
The reduce() method performs an operation on the array and returns one result, like getting the total amount of all the items in a cart.
The sort() method sorts items based on a property and returns a new array
The includes() method determines whether an array includes a certain value among its entries,
returning true or false as appropriate