chevron_left
459 points
12 7 5

String filter javascript

In this article let us decipher how to filter an array of strings on a specific condition.

An array in Javascript is a collection of elements of dissimilar data types. An array can hold elements of different data types unlike the other programming languages like Java where an array stores homogeneous elements.

An example of a string array is as follows:

const flowers=["Lotus","Jasmine","Roses"];

Here, flowers is an array of strings.

Now, let us perceive a method filter() which is used to filter out the strings from an array on a given condition.

filter()

The filter() method returns an array of all the elements that satisfies the specified function.

Syntax

The method filter() can be defined in different forms.


//Arrow function
filter((element) => { ... } )
filter((element, index) => { ... } )
filter((element, index, array) => { ... } )

Here,

element is the element being processed on the array specified.
index is the index of an element being processed on the array specified.
array is the name of the array.

index and array are the optional arguments.


// Callback function
filter(callbackFn)
filter(callbackFn, thisArg)

Here,

callbackFn is a function on which each element is tested.
thisArg is optional and holds value this when executing in a given array.


// Inline callback function
filter(function callbackFn(element) { ... })
filter(function callbackFn(element, index) { ... })
filter(function callbackFn(element, index, array){ ... })
filter(function callbackFn(element, index, array) { ... }, thisArg)

Let us consider few examples which gives perspicuous idea of the above exposition:

  1. Filter out the even numbers from given array of integers.


const arrnum=[12,33,66,55,77,-44,-88,-54];

const result = arrnum.filter(num => num % 2 ==0 );

console.log(result);

Here, we are implementing the arrow function which we discussed heretofore.

The variable result holds an array of elements that satisfies the given condition. The output of the code snippet is

[ 12, 66, -44, -88, -54 ]

  1. Filter the strings which starts with "dd" and ends with "kk" from a string array.


function filterString(name){

if( name.startsWith("dd") && name.endsWith("kk"))

{
   return name;<br>

}

else return false;

}

const words = ['ddspraykk', 'ddlimitkk', 'elite', 'ddexuberantkk', 'destruction', 'present'];

console.log(words.filter(filterString));


Here, we are using a callback function which performs the test on each array element if it satisfies the given condition. The output of this code snippet is :

[ 'ddspraykk', 'ddlimitkk', 'ddexuberantkk' ]

Hope this helps!!

More Posts

Javascript insert character into string at position DivyaN - Oct 18
Remove duplicate characters in a string javascript DivyaN - Oct 13
Javascript parsedouble DivyaN - 23 hours
Math.sum javascript DivyaN - 1 day
Javascript Hide button DivyaN - 1 day
Javascript for loop array DivyaN - 3 days
How to check if a button is clicked in javascript DivyaN - Oct 18
Button that redirects to another page html - Javascript DivyaN - Oct 18
Javascript print DivyaN - Oct 12
Javascript count occurrences in an array DivyaN - Oct 11