callback: callback function to be called for each element of the array arr
- previousValue: Value returned by previous function call or initial value.
- currentValue:Value of current array element being processed
- currentIndex:Index of the current array element being processed
- array: The original array on which reduce is being called.
initialValue(optional): Initial value to be used as previousValue when callback function is invoked for first time.
Sample Use Cases:
- To find sum of an array of integers.
- Given an array of objects containing addresses of sales locations and find the total sales done in NY
In the above example, an initial value 0 is provided while calling the Array.reduce() on arr. In this case it is necessary to provide an initial integer value. This is because for each iteration of callback function, the variable previousValue has to have an integer value and not whole object. If we don’t pass the initialValue as 0, then for first iteration, the previous value will become the whole object and will try to add an integer value to it, thereby giving junk output.
Description:Array.prototype.concat() is used to concatenate an array with another array/value. It does not change any existing array, instead, it returns a modified array. It accepts both new arrays and values as an argument which it concatenates with the array calling the function and returns the resultant array.
NewArray = Array1.concat(value1[, value2[, ...[, valueN]]])
valueN : New array or value to be concatenated to Array1.
Sample Use Case:
Concatenate 2 integer arrays:
Concatenate 3 integer arrays:
Concatenate an array with individual integer values:
Concatenate an array with another array and other multiple values:
Description: Array.prototype.sort() is used to sort an array. It accepts an optional argument compareFunction which defines the criteria to determine which element of the array is small and which is bigger. The compareFunction accepts 2 arguments i.e. the values being compared. If value1 is smaller then compare function should return a negative value. If value2 is smaller, compareFunction should return a positive value. In case compare function is not provided, the default compareFunction converts the array elements into strings and then compares those strings in Unicode point order.
compareFunction (optional): Function to define the sort order.
Sample Use Case:
Sort a simple integer array:
Sort a string array:
Sort another integer array:
Here, the order of resultant sorted array is not correct. This is because as we are using the default sortFunction, it converts all integers into strings and then compares them based on their Unicode value. Since 30 < 5 in Unicode, Hence the incorrect sort order.
Sort the integer array using sort function:
Here, the order is correct because we didn’t use the default sortFunction but our own sortFunction which compares the integer values to decide the sort order.
Sort an object array using sort function:
Sample Use Case:
All code snippets used in this article are available at https://github.com/hjaintech/GeeksForGeeks/blob/master/Array_Functions_3.html
Additionally, if you wish you dive deeper into the above functions, you can refer to the following official links.
About the author:
“Harshit is a technology enthusiast and has keen interest in programming. He holds a B.Tech. degree in Computer Science from JIIT, Noida and currently works as Front-end Developer at SAP. He is also a state level table tennis player. Apart from this he likes to unwind by watching movies and English sitcoms. He is based out of Delhi and you can reach out to him at https://in.linkedin.com/pub/harshit-jain/2a/129/bb5
If you also wish to showcase your blog here,please see GBlog for guest blog writing on GeeksforGeeks.