Skip to content Skip to sidebar Skip to footer

Javascript Chaining Methods Together

Lets say I have an array with multiple movies, these movie objects contain things like title/rating/etc... if I do something like so: let filteredMapList = arr.filter(function(obj)

Solution 1:

They chain and run exactly in the order they are in. It's a bit more obvious if you rewrite it like this:

let a = arr.filter(function(obj){
  return obj.Rating > 8.0;
});

let b = a.map(function(obj){
  return {title: obj.Title,rating: obj.Rating};
});

The filter happens first and goes to a, then map is called on a and goes to b with your result.

Chaining functions is the same, you're just skipping the step where you create a variable for each step.

Solution 2:

filter runs first, map second. map receives as an argument = what filter returned.

Post a Comment for "Javascript Chaining Methods Together"