How to Filter an Array in JavaScript ? Last Updated : 08 Nov, 2024 Comments Improve Suggest changes Like Article Like Report The array.filter() method is used to filter array in JavaScript. The filter() method iterates and check every element for the given condition and returns a new array with the filtered output.Syntaxconst filteredArray = array.filter( callbackFunction ( element [, index [, array]])[, thisArg]);Note: It is an in-built JavaScript method to filter an array. JavaScript // Given array const a = [ 1, 2, 3, 4, 5, 6]; // Filter the even numbers const a1 = a.filter( number => number % 2 === 0); // Display output console.log("Filtered Array:", a1); OutputFiltered Array: [ 2, 4, 6 ] Please Read JavaScript Array Tutorial for complete understanding of JavaScript Array.Table of ContentUsing for LoopUsing array.reduce() MethodUsing for LoopThe for loop iterates and repeatedly execute the given code until the condition is false. We can add the condition filter element in each iteration and use the array.push() to create new array of filtered values. JavaScript // Given array const a = [ 1, 2, 3, 4, 5, 6]; // Initialize empty array to store the results const a1 = [] for( let i = 0; i < a.length ; i++){ if( a[i] % 2 === 0 ) // Condition for even number a1.push(a[i]); // Push element to new array } // Display output console.log("Filtered Array:", a1) OutputFiltered Array: [ 2, 4, 6 ] Using array.reduce() MethodThe array.reduce() method executes a reducer function (that you will provide) on each element of the array, resulting in a single output value.Syntaxarray.reduce( function( acc , currentValue, currentIndex, arr), initialValue ) JavaScript // Given array const a = [ 1, 2, 3, 4, 5, 6]; // Filter the even numbers const a1 = a.reduce((acc, number) => { if (number % 2 === 0) { acc.push(number); // Only push the number if it is even } return acc; }, []); // Initialize acc as an empty array // Display output console.log("Filtered Array:", a1); OutputFiltered Array: [ 2, 4, 6 ] Comment More infoAdvertise with us Next Article How to Filter an Array in JavaScript ? R rituali8i63 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads JavaScript Array filter() Method The filter() method creates a new array containing elements that satisfy a specified condition. This method skips empty elements and does not change the original array.Create a new array consisting of only those elements that satisfy the condition checked by canVote() function.JavaScript// JavaScrip 3 min read How to filter Nested Array using Key in JavaScript ? Filtering a nested array in JavaScript involves the process of selectively extracting elements from an array of objects, where each object contains another array. We will discuss how can we filter Nested Array using the key in JavaScript. Filtering a nested array based on a key in JavaScript can be 3 min read How to filter an array of objects in ES6 ? In this article, we will try to understand how we could filter out or separate certain data from the array of objects in ES6.Let us first try to understand how we could create an array of objects by following certain syntax provided by JavaScript.Table of ContentJavascript Array of ObjectsJavascript 4 min read How to use map() filter() and reduce() in JavaScript? The map(), filter(), and reduce() are the array functions that allow us to manipulate an array according to our logic and return a new array after applying the modified operations on it. 1. JavaScript map() MethodThe map() method in JavaScript is used to create a new array by applying a function to 2 min read How to remove falsy values from an array in JavaScript ? Falsy/Falsey Values: In JavaScript, there are 7 falsy values, which are given below falsezero(0,-0)empty string("", ' ' , ` `)BigIntZero(0n,0x0n)nullundefinedNaNIn JavaScript, the array accepts all types of falsy values. Let's see some approaches on how we can remove falsy values from an array in Ja 6 min read How to Get all Elements Except First in JavaScript Array? Here are the various methods to get all elements except the first in JavaScript Array1. Using for loopWe will use a for loop to grab all the elements except the first. We know that in an array the first element is present at index '0'. JavaScriptconst a1 = [1, 2, 3, 4, 5]; const a2 = []; let k = 0; 4 min read How to Create a Filter Table with JavaScript? Filter tables are commonly used in web applications to organize and display tabular data efficiently. It allows users to search and filter through large datasets easily. In this tutorial, we will go through the steps to create a filter table with JavaScript. ApproachFirst, create the basic HTML stru 3 min read How to Remove Duplicate Objects from an Array in JavaScript? In JavaScript, it's a common example that the arrays contain objects and there might be a possibility that the objects may or may not be unique. Removing these duplicate objects from the array and getting the unique ones is a common task in Web Development. These are the following approaches: Table 2 min read JavaScript - How to Remove an Element from an Array? Removing elements from an array is a fundamental operation in JavaScript, essential for data manipulation, filtering, and transformation. This guide will explore different methods to efficiently remove elements from an array, enhancing your understanding and capability in handling arrays.1. Using po 3 min read How to define custom Array methods in JavaScript? JavaScript arrays are versatile data structures used to store collections of items. When you want to iterate through the elements of an array, it's common to use loops like for, for...of, or forEach. However, there's a nuance when it comes to iterating through arrays that have additional properties 2 min read Like