7 javaScript Array methods you should know
Tags: javascript, webdev, beginners, programming
Published: 2021-10-11 13:11:17

Arrays are one of the most common things a programmer uses or is likely to come across in a project. In this regard the array method we are going to look into should come in handy.
 We are going to use a single array for our examples 

```javascript
const clubs = [
  { name: "All-stars", fans: 20000 },
  { name: "Bay", fans: 30000 },
  { name: "C-stars", fans: 25000 },
  { name: "D-pillars", fans: 40000 },
  { name: "Clos", fans: 60000 },
  { name: "Magic", fans: 45000 }
]
```

Let us take a look at this methods and what they do to an array
##### filter
The filter method is used to filter out or remove all elements from an array that affirms the subject in the proposition logic and they are returned in a new array without altering the original array
for example

```javascript
const filterClub = clubs.filter((item) => {
  return item.fans <= 30000;
});
```

All the clubs having fans of less than or equal to 30000 fans are going to be returned to a new array.

![Screenshot (91)](https://user-images.githubusercontent.com/23004266/136793752-642ffd60-6877-4de0-9f95-655106c33168.png)

The filter method is a simple method to use.  It returns true or false for each item. If the item is true, it is included in the new array and if it is false it is not included. The filter method does not change the array or object it is being filtered over. This method is convenient because we do not have to worry about the old array being changed when using it subsequently.

##### map
This method allows the taking of an array and converting it to a new array so that all items in the array are going to look slightly different. Let us say we want to get the names of every clubs in the array sample. We can use the map method for this.
Example

```javascript
const clubNames = clubs.map((item) => {
  return item.name
});
```

![Screenshot (92)](https://user-images.githubusercontent.com/23004266/136794128-9061b13f-9b3b-4f23-ad42-5312a846758e.png)

We get a new array that prints out the names of the club in the original array without altering the original array. This is super convenient when you want to get the items in an object or the keys of an object or convert an array from one form to another. It has millions of uses.

#### find
This method allows a single object to be found in an array of objects. The method takes a single item as a parameter and returns the first item that returns true for the statement.

```javascript
const findClub = clubs.find((item) => {
  return item.name === "All-stars"
});
```

![Screenshot (93)](https://user-images.githubusercontent.com/23004266/136794411-39d02d01-230d-4e89-bf1e-fac9de361276.png)

##### forEach
This method does not return anything unlike the methods we covered previously. It works very similarly to a forLoop but it takes a function instead and takes a single parameter 

```javascript
clubs.forEach((item) => {
  console.log(item.name);
});
```

For every single element inside the array, it prints out the names. The method makes working with an array where you have to loop through them much easier so that you don't have to write clunky, long forLoop syntax.

##### some
This function does not return a brand new array. Instead what it does is to return true or false. We can check if some items in the array affirms or denies the subject in the proposition logic. example

```javascript
const highestFans = clubs.some((item) => {
  return item.fans <= 30000
});
```

![Screenshot (94)](https://user-images.githubusercontent.com/23004266/136794833-9c90bc54-f70f-403d-aecd-629ad492cf30.png)


It checks if any item value returns true and returns the first item that matches the criteria.

##### every 
This method checks if every single item in the array affirms the subject proposition logic and returns true or false
example

```javascript
const highestFans = clubs.every((item) => {
  return item.fans <= 30000
});
```

![Screenshot (95)](https://user-images.githubusercontent.com/23004266/136795110-f3d3b6d8-2c96-4034-9e56-eb550e1e461f.png)


##### reduce
This method performs an operation on the array and returns a combination of all the different operations. To get the total of all the fans in our clubs array we use the reduce method in the following way

```javascript
const totalFans = clubs.reduce((x, item) => {
  return item.fans + x;
}, 0);
```

![Screenshot (96)](https://user-images.githubusercontent.com/23004266/136795286-40a8f964-e2b1-4e90-81e7-58e12bdecc42.png)


It takes a property and an item we want the property to be reduced to. It also takes a second parameter which is where we want to start the reduce from. In our case it starts from 0.