How to use split and filter methods In Javascript
In this approach, we split the input string into an array of words using the split() method and then filter the array to retrieve the words whose length is greater than the given value ‘k’ using the filter() method.
Syntax:
let res = str.split(" ").filter(word => word.length > k).join(" ");
Example: The below example uses split and filter methods to find words that are greater than the given length k in JavaScript.
Javascript
let str = "hello geeks for geeks is computer science portal" ; let k = 4; let res = str.split( " " ) .filter(word => word.length > k); console.log(res.join( " " )); |
Output
hello geeks geeks computer science portal
JavaScript Program to Find Words which are Greater than given Length k
In JavaScript, to find words that are greater than the given length k, we need to split an array into words and then filter out words based on their lengths.
Table of Content
- Using split and filter methods
- Using forEach and an empty array
- Using RegEx
Contact Us