Phil
Phil

Reputation: 175

Combining filter() and map () methods in javascript

I have been working through a study guide on Github and I am trying to work through a problem that chains filter and map to get the answer. So, I have an array and I'm supposed to use the two methods to select the ids of the videos with a rating of 5.0. Here's the array:

function() {
var newReleases = [
    {
        "id": 70111470,
        "title": "Die Hard",
        "boxart": "http://cdn-0.nflximg.com/images/2891/DieHard.jpg",
        "uri": "http://api.netflix.com/catalog/titles/movies/70111470",
        "rating": 4.0,
        "bookmark": []
    },
    {
        "id": 654356453,
        "title": "Bad Boys",
        "boxart": "http://cdn-0.nflximg.com/images/2891/BadBoys.jpg",
        "uri": "http://api.netflix.com/catalog/titles/movies/70111470",
        "rating": 5.0,
        "bookmark": [{ id:432534, time:65876586 }]
    },
    {
        "id": 65432445,
        "title": "The Chamber",
        "boxart": "http://cdn-0.nflximg.com/images/2891/TheChamber.jpg",
        "uri": "http://api.netflix.com/catalog/titles/movies/70111470",
        "rating": 4.0,
        "bookmark": []
    },
    {
        "id": 675465,
        "title": "Fracture",
        "boxart": "http://cdn-0.nflximg.com/images/2891/Fracture.jpg",
        "uri": "http://api.netflix.com/catalog/titles/movies/70111470",
        "rating": 5.0,
        "bookmark": [{ id:432534, time:65876586 }]
    }
];

Now My code is as follows:

  newReleases.filter(function(video){
    return video.rating == 5.0;
  }).map (function(video) {
    return { id: video.id } 
  });
}

My thought process is I first call the filter method and apply a test (video.rating == 5.0) and return the elements that pass the test. Then I use the map method to apply a function to the elements that passed the test to return what I want, the ids of the videos that have a 5.0 rating, by having map return 'video.id'. Now when I execute, I get this error:

TypeError: 'undefined' is not an object (evaluating 'videoids.sortBy')

This error code is really throwing me off, because I don't see a 'videoids' variable in the array and it doesn't appear that I need to apply a .SortBy.

So I am asking for clarification on the error code please. I have also tried this code in a number of different fashions and have not come up with the correct one, so I would really appreciate a walk through of what I am not doing. The reason I feel confident in my code is because I have applied a very similar code to a very similar exercise in [Eloquent Javascript] at the end of Chapter 5 (Exercise 5.2 Mother-child age difference). I have spent hours (more than I'd like to admit) stuck on this and I have browsed stack overflow for about two hours. I could easily press the get answer button but I am afraid that I won't learn anything by doing that.

Could someone please assist me?

Upvotes: 1

Views: 1143

Answers (2)

Amir Raminfar
Amir Raminfar

Reputation: 34149

You must be doing something wrong because this works me. See https://jsfiddle.net/887mfh10/1/.

var newReleases = [
    {
        "id": 70111470,
        "title": "Die Hard",
        "boxart": "http://cdn-0.nflximg.com/images/2891/DieHard.jpg",
        "uri": "http://api.netflix.com/catalog/titles/movies/70111470",
        "rating": 4.0,
        "bookmark": []
    },
    {
        "id": 654356453,
        "title": "Bad Boys",
        "boxart": "http://cdn-0.nflximg.com/images/2891/BadBoys.jpg",
        "uri": "http://api.netflix.com/catalog/titles/movies/70111470",
        "rating": 5.0,
        "bookmark": [{ id:432534, time:65876586 }]
    },
    {
        "id": 65432445,
        "title": "The Chamber",
        "boxart": "http://cdn-0.nflximg.com/images/2891/TheChamber.jpg",
        "uri": "http://api.netflix.com/catalog/titles/movies/70111470",
        "rating": 4.0,
        "bookmark": []
    },
    {
        "id": 675465,
        "title": "Fracture",
        "boxart": "http://cdn-0.nflximg.com/images/2891/Fracture.jpg",
        "uri": "http://api.netflix.com/catalog/titles/movies/70111470",
        "rating": 5.0,
        "bookmark": [{ id:432534, time:65876586 }]
    }
];

var ids = newReleases.filter(function(video){
    return video.rating == 5.0;
  }).map (function(video) {
    return { id: video.id } 
  });

alert(JSON.stringify(ids)); // see console

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386560

Please add return. Otherwise you are undefined returning.

return newReleases.filter(function(video) {
    //...

Upvotes: 2

Related Questions