Map vs filter methods

Map vs filter methods
0

#1

I am trying to understand the difference between these 2 methods. Consider an array of objects as follows:

var newReleases = [ { "id": 70111470, "title": "Die Hard", }, { "id": 654356453, "title": "Bad Boys", }, { "id": 65432445, "title": "The Chamber", }, { "id": 675465, "title": "Fracture", } ]; return newReleases. filter(function(video) { return video.rating === 5.0; }). map(function(video) { return video.id; });

So, from what I have understood, filter only filters items of an array based on the given condition. So, it returns an array with objects/items that fulfill the particular condition(in this case, movies with ratins equal to 5.0) and map returns only that particular property within each object (such as id) as an array.

Am I correct in my understanding of maps and filters? Working with an array of objects is more confusing than just an array with plain items.


#2

I am hardly an expert on this and am still learning but will attempt an answer.

Filter returns array elements that evaluate to true with the given expression (like 5 star reviews)

Map will carry out the expression in each element of the array and return a new value (number or object or…) for every element.

Map can be a function that creates an entirely new object not just do calculations on the array elements.

So filter returns only some where as map returns all


#3

.filter returns an array based on the originial one whose elements yield a “truthy” result when passed to the callback function (or in other words, satisfy the condition).

.map returns an array whose elements are a result of applying the callback function to each element in the array, in the same order as the original.