<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Joshua Campos</title>
    <description>The latest articles on Forem by Joshua Campos (@jc0808).</description>
    <link>https://forem.com/jc0808</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F922027%2F32b2e06c-166d-4430-aee4-04564d4d6af1.png</url>
      <title>Forem: Joshua Campos</title>
      <link>https://forem.com/jc0808</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jc0808"/>
    <language>en</language>
    <item>
      <title>How to Create Migrations with Rails</title>
      <dc:creator>Joshua Campos</dc:creator>
      <pubDate>Wed, 26 Oct 2022 17:05:51 +0000</pubDate>
      <link>https://forem.com/jc0808/how-to-create-migrations-with-rails-3702</link>
      <guid>https://forem.com/jc0808/how-to-create-migrations-with-rails-3702</guid>
      <description>&lt;p&gt;The purpose of creating migrations is to alter or modify the data in our database. Using migration is more easier that writing SQL commands to create, add or remove tables, columns or entries to the database schema. For this blog I am going to show you how to create migrations using rake tasks.&lt;/p&gt;

&lt;p&gt;Let's say we want to create a table to store users information for a web application.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, we will run this command:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;$ rake db:create_migration NAME=create_users_table&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;By running this command a new file will be generated in db/migrations that will be called &lt;strong&gt;20221026_create_users_table&lt;/strong&gt;. The beginning of the files name is the timestamp of when the file was created so that the migrations can run in the correct order.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now, lets take a look at the new migration file that we just created:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwgrethqh53ymctsfsbi1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwgrethqh53ymctsfsbi1.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we have a class called &lt;strong&gt;CreateUsersTable&lt;/strong&gt; and it inherits from Active Records Migration module. Inside the class there is a method called &lt;strong&gt;change&lt;/strong&gt;, this method is used to update the database schema.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Next, inside the method &lt;strong&gt;change&lt;/strong&gt; we will create a new table called "users" like this:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq8l5ejb319awcaprp4k4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq8l5ejb319awcaprp4k4.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we add the &lt;strong&gt;create_table&lt;/strong&gt; method and we passed the name of the table we are creating as a symbol and then we write a code block with the columns names and their types for our table &lt;strong&gt;"users"&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lastly, all we have to do is run the migrations by running this command in the console:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;$ rake db:migrate&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After running this command the database will be generated along side a new file called schema.rb which will contain the skeleton of our database.&lt;/p&gt;

&lt;p&gt;Lets take a look:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvrtqvz7dxkv4odp9t2da.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvrtqvz7dxkv4odp9t2da.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we want to update our database schema to add a column that we forgot to add in our first migration we will need to create a new migration file like this:&lt;/p&gt;

&lt;p&gt;We create a new migration:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ rake db:create_migration NAME=add_column_to_users&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp6qgq4zm74bu5sjpsu1l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp6qgq4zm74bu5sjpsu1l.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We then add a column:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcxr0izmtzdxwwiskup40.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcxr0izmtzdxwwiskup40.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then we run the migration&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ rake db:migrate&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And now we can see our new added column called address to our schema file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6rxab6w6qhvmft56v8dy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6rxab6w6qhvmft56v8dy.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
    </item>
    <item>
      <title>How to redirect to another page when we click a button on a form with react-router-dom@v5</title>
      <dc:creator>Joshua Campos</dc:creator>
      <pubDate>Wed, 05 Oct 2022 14:45:32 +0000</pubDate>
      <link>https://forem.com/jc0808/how-to-redirect-to-another-page-when-we-click-a-button-on-a-form-with-react-router-domv5-49i1</link>
      <guid>https://forem.com/jc0808/how-to-redirect-to-another-page-when-we-click-a-button-on-a-form-with-react-router-domv5-49i1</guid>
      <description>&lt;p&gt;Lets say we create a form for a login page and when we clicked the div element to sign up, we want to redirect our page to "/createuser" page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgre1gsl5o1sapayvodkr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgre1gsl5o1sapayvodkr.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
We can import the useHistory hook from react-router-dom:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6lqo7b5k1lzesjlzof6y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6lqo7b5k1lzesjlzof6y.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
We can declare the useHistory in a const variable:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqujesc5510k3xwkfw1oa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqujesc5510k3xwkfw1oa.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
We add an event listener to our div, so that it can listen for a click and then passed in a callback function and then push the url route we want our form to redirect to once we click the sign up div to the useHistory that we declared inside the navigation variable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpn1mo53tolwvo0bn38zm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpn1mo53tolwvo0bn38zm.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note that the useHistory() is for react-router-dom@5&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you are using the newer version then instead of using the useHistory Hook you can use the useNavigate Hook.&lt;/p&gt;

&lt;p&gt;for example: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For the V5&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const navigate = usehistory();
navigate.push("/");


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;For the V6&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const navigate = useNavigate();
navigate("/")


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>react</category>
    </item>
    <item>
      <title>How to Use the Filter() Method</title>
      <dc:creator>Joshua Campos</dc:creator>
      <pubDate>Wed, 07 Sep 2022 06:41:57 +0000</pubDate>
      <link>https://forem.com/jc0808/how-to-use-the-filter-function-3pfh</link>
      <guid>https://forem.com/jc0808/how-to-use-the-filter-function-3pfh</guid>
      <description>&lt;p&gt;The Filter() method finds and returns a list of elements that meet a certain condition. It takes in a callback function.&lt;/p&gt;

&lt;p&gt;Here we have an array of group of peoples objects, which store their first name, last name, favorite color, and favorite food. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cIaZ6YK8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ups9uvgpl028v0zenh0g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cIaZ6YK8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ups9uvgpl028v0zenh0g.png" alt="Image description" width="782" height="1948"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's say we want to return only the first and last names of the people that loves pizza with a string that says for example: &lt;strong&gt;'Carlos, Herrera likes pizza!&lt;/strong&gt;. For this we could use the &lt;strong&gt;filter()&lt;/strong&gt; method which would go through each object of the array and return the objects with people whose favorite food is pizza. Like this: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
First, we can create a function called &lt;strong&gt;getPizzaLovers()&lt;/strong&gt; that will take one arguments called person, since we are going through each object in the array. This function will serve as our callback function. Here we will use an if statement to check and return if the person's favorite food is ‘Pizza’.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xFixlQvf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k3vhmygj5gbskroae139.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xFixlQvf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k3vhmygj5gbskroae139.png" alt="Image description" width="880" height="283"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
Second, we create a const variable named &lt;strong&gt;filtered&lt;/strong&gt; were we are going to call the &lt;strong&gt;filter()&lt;/strong&gt; method and store the filtered objects. Here we invoke the &lt;strong&gt;filter()&lt;/strong&gt; method to group and we pass our callback function &lt;strong&gt;getPizzaLovers()&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Xd8Wx8fz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x131xalyehx3ogtqchfg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Xd8Wx8fz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x131xalyehx3ogtqchfg.png" alt="Image description" width="880" height="103"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
Lastly, we console log the result. Since the &lt;strong&gt;filter()&lt;/strong&gt; function is going to return us an array of objects, one way we could print out our string is by using a method called &lt;strong&gt;ForEach()&lt;/strong&gt;. Here we invoke the &lt;strong&gt;forEach&lt;/strong&gt; on the filtered objects and for each person in the array of objects it will console.log our string &lt;strong&gt;’FIRSTNAME LASTNAME loves FAVORITEFOOD!’&lt;/strong&gt;. Like this: &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ui2DW65o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xs212lj9o4sqa1110urx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ui2DW65o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xs212lj9o4sqa1110urx.png" alt="Image description" width="880" height="107"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we run the program:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OeeJ8Efl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bloffvr9kd43snx50adb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OeeJ8Efl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bloffvr9kd43snx50adb.png" alt="Image description" width="880" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see, we got 3 names for which their favorite food is Pizza, while the other names were filtered out.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: There are other ways to print out our string using for loops, but the forEach() method is a more easy way to iterate through each element of an array.&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;Another Example:&lt;/p&gt;

&lt;p&gt;Let’s say we want to only return a string on only the people whose last name starts with ‘M’ and likes 'Sushi'.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
First, we can create a function called &lt;strong&gt;getNames()&lt;/strong&gt; that will take one argument called person, since we are going through each object in the array.This function will serve as our callback function. Only that for this one we are going to use an if statement to check and return if the last name of the person starts with ‘M’ and if their favorite food is 'Sushi'.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LaodMwhp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xga25hne40ba0pb84y9u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LaodMwhp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xga25hne40ba0pb84y9u.png" alt="Image description" width="880" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
Second, we create a const variable named &lt;strong&gt;filtered&lt;/strong&gt; were we are going to call the &lt;strong&gt;filter()&lt;/strong&gt; method and store the filtered objects. Here we invoke the &lt;strong&gt;filter()&lt;/strong&gt; function to group and we pass our callback function &lt;strong&gt;getNames()&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Jtxn3DO1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vct0777ikraa9lj2dhve.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Jtxn3DO1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vct0777ikraa9lj2dhve.png" alt="Image description" width="880" height="166"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
Lastly, we console log the result. Since the &lt;strong&gt;filter()&lt;/strong&gt; method is going to return us an array of objects, one way we could print out our string is by using a method called &lt;strong&gt;ForEach()&lt;/strong&gt;. Here we invoke the &lt;strong&gt;forEach&lt;/strong&gt; on the filtered objects and for each person in the array of objects it will console.log our string &lt;strong&gt;’FIRSTNAME LASTNAME loves FAVORITEFOOD!’&lt;/strong&gt;. Like this: &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--J9rAWZNf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u1eqxs6ehxq3jzzteojp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--J9rAWZNf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u1eqxs6ehxq3jzzteojp.png" alt="Image description" width="880" height="116"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we run the program:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D6qHGXnm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/og9p8uy92c295aqm7qih.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D6qHGXnm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/og9p8uy92c295aqm7qih.png" alt="Image description" width="880" height="213"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see we got two names, even though there is another person that has a last name that starts with 'M'. The reason it did not return it, was because that person's favorite food was not 'Sushi' therefore that name got filtered out.&lt;/p&gt;




&lt;p&gt;Let's say we want to return an array of objects with only the people that like the color 'Pink'.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, we can create a function called &lt;strong&gt;getPinkLover()&lt;/strong&gt; that will take one argument called person, since we are going through each object in the array.This function will serve as our callback function. Only that for this one we are going to use an if statement to check and return the person's object if the favorite color is 'Pink'.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vJfjQoA8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/30qy6klnac40dszhy2iy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vJfjQoA8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/30qy6klnac40dszhy2iy.png" alt="Image description" width="880" height="344"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Second, we create a const variable named &lt;strong&gt;filtered&lt;/strong&gt; were we are going to call the &lt;strong&gt;filter()&lt;/strong&gt; method and store the filtered objects. Here we invoke the &lt;strong&gt;filter()&lt;/strong&gt; function to group and we pass our callback function &lt;strong&gt;getPinkLover()&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8_h1E_OR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vx8ci41ehpy90oortj4k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8_h1E_OR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vx8ci41ehpy90oortj4k.png" alt="Image description" width="880" height="127"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lastly,Since the &lt;strong&gt;filter()&lt;/strong&gt; method is going to return us an array of objects, which is what we want. The only thing we need to do is to console log it.  Like this: &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SWcHy1N7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0acm344ljhsbyb65ftg1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SWcHy1N7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0acm344ljhsbyb65ftg1.png" alt="Image description" width="600" height="152"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we run it &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9uFvYqbi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/70hr1wz73izzt6aeqiib.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9uFvYqbi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/70hr1wz73izzt6aeqiib.png" alt="Image description" width="782" height="344"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we can see that we got one person's object that likes the color pink. We can also see their first name, last name, favorite color, and favorite food.&lt;/p&gt;

&lt;p&gt;Array methods like the &lt;strong&gt;filter()&lt;/strong&gt; method is one way to write more easy and efficient code. Which means we don't need to use for...of or for...in loop.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
