Will NFTs Power The Metaverse?

You might have heard the term Non Fungible Tokens (NFT) used a lot in the media recently. Although they’ve been around for many years, interest in the space has rapidly increased after reports of…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




React and .filter

How to use the filter method is an important skill in building your app. Basically filter allows data to be parsed through so that you can “filter” certain data. If you have an array of numbers and you only want numbers greater than 4 to be shown, then you can program that with this method.

Let’s jump into some examples of how to filter through an array. Let’s say we want to make a list of numbers of apples but the only numbers we want to return are numbers greater than 3.

It’s going to filter out the numbers that only pass the callback function which is numbers only greater than 3. Now, it doesn’t look all that useful in the console but we can clean it up by chaining on a .map.

All I did here was take that same num variable and add it into the .map method, add some text, and wrap it all in li tags. Pretty cool stuff.

Now let’s try spice things up a little:

What we did was to filter out names that only were greater than 3 letters in length. Next we’ll see how to filter through an array of objects. This’ll give us more options and ability to move our data around.

Let’s say we have this array of objects including employee names with their ages as shown above. If we want to return an array that includes only those employees who are over the age of 30, we can do:

You can see below that I haven’t even finished my return statement but it already has filtered out the under 30 employee, showing the 3 points for the employees over 30:

Here’s the final product:

The filter method is essentially saying for every employee, check the age key, and only return those are over 30. Then the .map method is chained onto this information from the filter and is programming that every that is over 30, it should be in an li tag and display the name. And that’s the basics of filtering!

Add a comment

Related posts:

Mining the Potential of Miniature Quilts

With an air of tranquility, the village in Virginia sprawls along the Catoctin Creek. To the west, the Blue Ridge Mountains rise. Less than fifty miles to the east, bureaucrats and powermongers elbow…

A LITTLE SECRET OF SUCCESS!!!

Recently I have red a chapter “ Do This And You’ll be Welcome Anywhere” written by Dale Harbison Carnegie, He was an American writer and lecturer and the developer of famous courses in…

The Heart of Star Atlas

Valugar woke up in his bed, totally destroyed after the last night. He had spent too much time in his favorite outpost on the edge between the medium risk zone and the outer and deeper space. Oh…