In this post, we are going to learn different ways of how to Find longest string in JavaScript ES6 array by using different array built in methods array.map(),array.filter(),array.reduce(),array.sort() and spread operator.
1. array.filter() to Find Longest string in array
In this example, we are filtering the longest length element of the string array using an array.map() method returns a new array populated with the result by calling the given function on each element of the array.
The array. filter() method returns a new array with the element that passess the given condition implemented by a callback function.
To return an element instead of the array replace the return statement with “return Str[0]”.
var mutiLineStr = [
'Welcome',
'How',
'visiting',
'Learning!',
'Happy'
]
function Find_longStr(myarry) {
var max = myarry[0].length;
myarry.map(item => max = Math.max(max, item.length));
Str = myarry.filter(item => item.length == max);
return Str;
}
console.log(Find_longStr(mutiLineStr))
Output
["Learning!"]
2. Spread operator with array.fillter() to Find longest string
In this Javascript program, we have used the spread operator(. . .) with the array. map() and array. filter() to get the longest string in a string array.This is how we find the longest string in javascript ES6.
JavaScript ES6 Program to find the longest string in array
//define a an arrayof string
var mutiLineStr = [
'Welcome',
'How',
'visiting',
'Learning!',
'Happy'
]
//return the longest string element from array
var longestStr_inArr = mutiLineStr => {
let MaxLng_Str = Math.max(...mutiLineStr.map( item => item.length))
return mutiLineStr.filter(item => item.length === MaxLng_Str)[0]
}
console.log(longestStr_inArr(mutiLineStr))
Output
Learning!
3. foreach loop to Find longest string in JavaScript array
In this JavaScript program, To find the longest string in the array, We are using a foreach loop to iterate through all elements of the array.
We have defined a variable “largest_Str” that holds the longest string and compared the next element with “largest_Str” on each iteration and at last, it returns the longest string from the array the iterate will over.
var Str_Array = [
'Welcome',
'How',
'visiting',
'Learning!',
'Happy'
]
var largest_Str = '';
Str_Array.forEach(function(item) {
if (item.length > largest_Str.length)
largest_Str = item
});
console.log(largest_Str)
Output
Learning!
4. for-of loop to Find longest string in JavaScript array
In this JavaScript program to find the longest string in the array. We are using a for-of loop to iterate through all elements of the array.
We have defined a variable “largest_Str” that holds the longest string on each iteration and compared the next element with “largest_Str“, it returns the longest string from the array when the iteration is over.
var Str_Array = [
'Welcome',
'How',
'visiting',
'Learning!',
'Happy'
]
let largest_Str = '';
for (let item of Str_Array) {
if (item.length > largest_Str.length) largest_Str = item
}
console.log(largest_Str)
Output
Learning!
5. array.reduce() to Find Longest string in array
“The reduce()
the method executes a user-supplied “reducer” callback function on each element of the array, in order, to pass in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value”.
var Str_Array = [
'Welcome',
'How',
'visiting',
'Learning!',
'Happy'
]
const largest_Str = (Str_Array) => Str_Array.reduce(
(item, Strlen) => (Strlen.length > item.length ? Strlen : item),
'',
);
console.log(largest_Str(Str_Array))
Output
Learning!
6.array.sort() to Find longest string in JavaScript array
In this example, we will understand how to find the longest string in an array of strings using array.sort() method.The array. sort() method sort in place and return a new sorted array. The default order of sorting is ascending.
var Str_Array = [
'Welcome',
'How',
'visiting',
'Learning!',
'Happy'
]
var longest = Str_Array.sort(
function (a_item, b_item) {
return b_item.length - a_item.length;
}
)[0];
console.log(longest)
Output
Learning!
Summary
In this post we have learned different ways how to Find longest string in array JavaScript ES6 using different array built in method array.map(),array.filter(),array.reduce(),array.sort() and spread operator.