# freeCodeCamp Challenge Guide: Use the filter Method to Extract Data from an Array

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-the-filter-method-to-extract-data-from-an-array/18179>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [July 7, 2016, 8:15pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-the-filter-method-to-extract-data-from-an-array/18179 "2016-07-07T20:15:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [July 7, 2016, 8:15pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-the-filter-method-to-extract-data-from-an-array/18179/1 "2016-07-07T20:15:25Z")

</div>

# [Use the filter method to extract data from an array](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/use-the-filter-method-to-extract-data-from-an-array)

* * *

## Hints

### Hint 1

This challenge is solved in 2 steps.

`Array.prototype.filter()` is used to filter the array so it’s left with elements that have imdbRating \> 8.0.

`Array.prototype.map()` can be used to shape the output to the desired format.

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> ```javascript
> const filteredList = watchList
> .filter(movie => {
> // return true it will keep the item
> // return false it will reject the item
> return parseFloat(movie.imdbRating) >= 8.0;
> })
> .map(movie => {
> return {
> title: movie.Title,
> rating: movie.imdbRating
> };
> });
> 
> ```
> 
> #### Code Explanation
> 
> First we `filter` through and only return the objects that meet the criteria. In this case the criteria is having an imdbRating of 8.0 or higher.  
> Then we `map` the objects to the desired format.

> **Solution 2 (Click to Show/Hide)**
>
> ```javascript
> const filteredList = watchList
> .filter(movie => movie.imdbRating >= 8.0)
> .map(movie => ({ title: movie["Title"], rating: movie["imdbRating"] }));
> // Only change code above this line
> console.log(filteredList);
> 
> ```

> **Solution 3 (Click to Show/Hide)**
>
> ```javascript
> // Only change code below this line
> const filteredList = watchList
> .filter(({ imdbRating }) => imdbRating >= 8.0)
> .map(({ Title: title, imdbRating: rating }) => ({ title, rating }));
> // Only change code above this line
> 
> console.log(filteredList);
> 
> ```

* * *

#### Relevant Links

- MDN: [Array.prototype.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)
- MDN: [Array.prototype.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
- MDN: [Destructuring assignment](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment)

---

<div class="post-metadata">

**Author:** ![andserg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/andserg/32/59041_2.png) [@andserg](https://forum.freecodecamp.org/u/andserg)\
**Post date:** [June 29, 2017, 2:17pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-the-filter-method-to-extract-data-from-an-array/18179/2 "2017-06-29T14:17:42Z")

</div>

Hi The only problem is that we need to correct conditions or text. Watch it out here

> **[Screenshot](https://prnt.sc/fpm1mc)**
>
> Captured with Lightshot

---

<div class="post-metadata">

**Author:** ![rugano](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rugano/32/93045_2.png) [@rugano](https://forum.freecodecamp.org/u/rugano)\
**Post date:** [July 24, 2017, 5:15pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-the-filter-method-to-extract-data-from-an-array/18179/3 "2017-07-24T17:15:02Z")

</div>

Hey fellow campers,here is how I did it:

var oldArray = [1,2,3,4,5,6,7,8,9,10];

// Only change code below this line.

var newArray = oldArray.filter(function(val) {  
return val \< 6;  
});

---

<div class="post-metadata">

**Author:** ![garnicha](https://avatars.discourse-cdn.com/v4/letter/g/aeb1de/32.png) [@garnicha](https://forum.freecodecamp.org/u/garnicha)\
**Post date:** [August 6, 2017, 7:42am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-the-filter-method-to-extract-data-from-an-array/18179/4 "2017-08-06T07:42:22Z")

</div>

the condition " return val \<= 5" isn’t working and it’s weird.

---

<div class="post-metadata">

**Author:** ![garnicha](https://avatars.discourse-cdn.com/v4/letter/g/aeb1de/32.png) [@garnicha](https://forum.freecodecamp.org/u/garnicha)\
**Post date:** [August 6, 2017, 7:45am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-the-filter-method-to-extract-data-from-an-array/18179/5 "2017-08-06T07:45:15Z")

</div>

> [@garnicha](#):
>
> the condition " return val \<= 5" isn’t working and it’s weird

and the condition " return val \< 6" also not working…

---

<div class="post-metadata">

**Author:** ![rugano](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rugano/32/93045_2.png) [@rugano](https://forum.freecodecamp.org/u/rugano)\
**Post date:** [August 6, 2017, 6:48pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-the-filter-method-to-extract-data-from-an-array/18179/6 "2017-08-06T18:48:24Z")

</div>

Hello @garnicha.The instruction says that we use "filter" to create/return a new array with all the values from oldArray which are less than 6.

So, simply put,our new array should not have a number which is 6 or more than 6.Like this:

var oldArray = [1,2,3,4,5,6,7,8,9,10];

// Only change code below this line.

var newArray = oldArray.filter(function(val){  
return val \< 6;

});

---

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [August 10, 2019, 1:19am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-the-filter-method-to-extract-data-from-an-array/18179/8 "2019-08-10T01:19:53Z")

</div>

Opened on August 10, 2019, 1:19am UTC



---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 7, 2025, 12:00pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-use-the-filter-method-to-extract-data-from-an-array/18179/9 "2025-01-07T12:00:49Z")

</div>

Closed on January 7, 2025, 12:00pm UTC


