How to use JavaScript filter() function Basics
Hey all, hope you all having a good day. This is my first writing on medium. Today I am going to talk about filter() function of JavaScript with examples. This will specially help you to understand how to use filter() function to get data from JSON. Lets start,
so lets say you have a user JSON file like this,
This list can have any amount data of user. Let say we want to filter out the people who are from country Bangladesh . so we can do,
So I showed two way to do this, one with vanilla JS and other with arrow function. I prefer arrow function as it looks clean and easy.
what we did there is, we matched each event.country or e.country with the name “Bangladesh” to get the filtered Bangladeshi User. The result will be like the gist show in Output.JS .
Now lets say you want the persons who are female and also from Bangladesh all you have to do is add another filter above it like this,
so you can see in the output we get all females. Now lets say I want to see Female user from Bangladesh whose age is less than 30. we can do is,
You can go on until you get the exact data you need.
This is basic level of filtering a JSON data. you can map() or reduce() it to get some extra job done.
Until Next Time. Have a good day :)