0

filter() vs map()

I have come across these two functions filter() and map() that take in a function and an iteration. Both seem to work the same. What is the difference between the two?

27th May 2020, 11:51 AM
BENJAMIN LIMBU SUBBA
BENJAMIN LIMBU SUBBA - avatar
2 odpowiedzi
+ 6
They both return a new array. map returns a new array of elements where you have applied some function on the element so that it changes the original element (typically). filter returns a new array of the elements of the original array (with no change to the original values). filter will only ret…
27th May 2020, 11:54 AM
Johar Khan
Johar Khan - avatar
+ 3
map() does not change or mutates the original array. Except of course you assigned the return value to the original variable. The difference between map() and filter() is simply that map() returns a new array of the same size but with changed elements while filter() returns an array of fewer but unchanged elements that meet a particular condition. In short, map changes values filter changes size See the docs Map https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map Filter https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
27th May 2020, 12:53 PM
Ore
Ore - avatar