Filter an array in JavaScript

You might want to filter an array in JavaScript by passing the filter criteria and return the filtered array. In this scenario , you can use the Array’s filter method as shown below.

How to Filter an array in JavaScript ?

var actors = new Array('vijay', 'ajith', 'surya', 'arya');
var retArray = actors.filter(function(item) {
    return item != 'arya';
});
console.log(retArray);

The filter method is part of the ECMAScript 5 and the function is passed as parameter to it. In the above function , the end array contains the values that are not arya.

image

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

You can flatten a 2-D array in JavaScript using the concat and apply method as shown in the below code...
Assume that you have an angle in degree and you wish to convert it to radians in JavaScript so that...
If you have a decimal number in JavaScript and want to convert it to its equivalent octal , hexadecimal and...