Difference between Javascript Map & Filter Method -
Program I:
var arr = [4, 24, 65, 13, 98];
var newArray = arr.map((i) => i + 20);
console.log(newArray);
Output:
(5) [24, 44, 85, 33, 118]
Program II:
var arr = [4, 24, 65, 13, 98];
var newFilter = arr.filter((i) => i + 20);
console.log(newFilter );
Output:
(5) [4, 24, 65, 13, 98]
Finally, Ouput indicates that the main difference in using the map & filter is that filter doesn't change the value of array element while map can have the updated values for the elements.
Map & Filter both method return the new array.
Map methos keeps all the indexes in the new Array while Filter keep only the true case index in new array.
No comments:
Post a Comment