Filter Method, callback Function, parameter pointing to the Object that is being filtered

Tell us what’s happening:
Hello,
I need some clarification with solution 2. This is a two part question.
First, to the map method, we’re passing a callback function that takes parameter (e). Later in a code, we see that parameter e is pointing to the object “watchList”. We are then using e to extract properties from “watchList”. Correct?
What is the connection between "e " and a “watchList” object? How does the program “know” that “e” parameter is pointing to the “watchList”?

Second, in the later part of the code, we’re filtering ratings equal to or above 8.0. However, code never converts rating string into rating floating points.
Why is conversion unnecessary?

I hope I was not too long :grinning:

Many thanks in advance.

Your code so far

WARNING.

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.


var filteredList = watchList
  .map(function(e) {
    return { title: e["Title"], rating: e["imdbRating"] };
  })
  .filter(e => e.rating >= 8);

console.log(filteredList);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36.

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

Link to the challenge:

Hello there,

I will start by pointing you to the relevant docs: Map, Filter

If you are confident enough, scroll through it, and get a sense of how the methods are made.

Otherwise, the first thing to recognise is that the methods are being called on the object. watchList has been created, and within its object class, it contains some inherent methods. This is why you will see the docs refer to the methods as Array.prototype.<nameOfMethod> -> the methods inherit in all objects of type array.

Now, if you go to the docs Polyfill section, you will see the definition of the function (method) map. The method extracts the object the method was called on:

var O = Object(this);

Then, the method performs the specified callback on each element of the object:

if (k in O) {
        //    method of O with argument Pk.
        kValue = O[k];
        // ii. Let mappedValue be the result of calling the Call internal 
        //     method of callback with T as the this value and argument 
        //     list containing kValue, k, and O.
        mappedValue = callback.call(T, kValue, k, O);
// T is an extra argument passed to map

Then, a new array is returned.

Once, this new array is returned, the filter method is called, and a very similar process is performed.

Why…JavaScript…

JavaScript does the conversion for you, provided you do not use strict equality checking. Type Coercion

Hope this helps

1 Like

Yes, it helps! Thanx.