Posts

Showing posts with the label reduce

What are filter, map and reduce functions in JavaScript?

These are in built functions which are available on arrays in JavaScript. In this article, we will talk about what these functions do and how they are used. filter – Array.prototype.filter This function returns a subset of an array containing those elements which satisfy a given condition in the form of a callback . Syntax newArray = someArray.filter(callback(element[, index, array])[, thisArg]) newArray is the filtered array. someArray is the original array. callback is the callback function. element is the current element of the array which is being processed by the filter function. index is the index of the current element. array is the original array. thisArg is the value to be used as this while executing the callback. If the callback function returns a truthy value then the element will be included in the new array otherwise it will be ignored. Examples Getting even numbers from a list of numbers let   numbers  = [ 1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,  9 ,