Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript Array filter() Method
Next article icon

ES6 | Array filter() Method

Last Updated : 28 May, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

The Array filter() is an inbuilt method, this method creates a new array with elements that follow or pass the given criteria and condition. Few Examples have been implemented below for a better understanding of the concept Syntax:

var newArray = arr.filter(callback(element[, index[, array]]) [, thisArg])

Parameter: This method accepts 2 parameters which was mentioned above and described below:

  • Callback: The function is a predicate, to test each element of the array. Return true to keep the element, false otherwise. It accepts three arguments:
    • element: The current element being processed in the array.
    • index(Optional): The index of the current element being processed in the array.
    • array(Optional): The array filter was called upon.
  • thisArg(Optional): Value to use as this when executing the callback.

Example 1: The filter function filters all the numeric values in the array greater than 5 

javascript




var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var result = numbers.filter(number => number > 5);
console.log(result);
 
 

Output :

[ 6, 7, 8, 9, 10 ]

Time complexity: O(n) 

Auxiliary Space: O(1)

Example 2: The filter function filters all the words in the array which have length greater than 5 

javascript




var words = ["hi", "hello", "hey", "apple", "watermelon",
            "lemon", "javascript"];
var result = words.filter(word => word.length > 5);
console.log(result);
 
 

Output :

[ 'watermelon', 'javascript' ]

Time complexity: O(n) 

Auxiliary Space: O(1)

Example 3: The filter function filters all invalid id of users from the array. 

javascript




var jsonarr = [
    {
        id: 1,
        name: "joe"
    },
    {
        id: -19,
        name: "john"
    },
    {
        id: 20,
        name: "james"
    },
    {
        id: 25,
        name: "jack"
    },
    {
        id: -10,
        name: "joseph"
    },
    {
        id: "not a number",
        name: "jimmy"
    },
    {
        id: null,
        name: "jeff"
    },
]
 
var result = jsonarr.filter(user => user.id > 0);
 
console.log(result);
 
 

Output:

[{"id":1,"name":"joe"},{"id":20,"name":"james"}, {"id":25,"name":"jack"}] 

Time complexity: O(n) 

Auxiliary Space: O(1)



Next Article
JavaScript Array filter() Method

I

IshjotSingh97
Improve
Article Tags :
  • JavaScript
  • Technical Scripter
  • Web Technologies
  • JavaScript-ES
  • Technical Scripter 2019

Similar Reads

  • Array Helper Methods in ES6
    Array helper methods in ES6 (JavaScript) are extremely useful for working with data stored in arrays. As a web developer, you often work with arrays, whether they contain simple numbers or complex objects with many attributes. These methods significantly simplify the manipulation of arrays, making i
    3 min read
  • Cypress - filter() Method
    Cypress is a popular JavaScript testing framework used for web automation testing. It provides a lot of useful methods to interact with web elements, one of which is the filter() method. In this article, we will discuss the filter() method in Cypress, its usage, syntax, arguments, and examples. Usag
    2 min read
  • Ember.js ArrayProxy filter() Method
    Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. C
    4 min read
  • 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. [GFGTABS] JavaScript
    3 min read
  • Array filter() Method - TypeScript
    The Array.filter() method in TypeScript creates a new array with elements that pass the test provided by a callback function. It does not modify the original array and accepts an optional thisObject for context within the callback function. Syntaxarray.filter(callback[, thisObject])Parameter: This m
    3 min read
  • Ember.js ArrayProxy filterBy() Method
    Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. C
    4 min read
  • Ember.js EmberArray filter() Method
    Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. C
    5 min read
  • JQuery | isArray() method
    This isArray() Method in jQuery is used to determines whether the argument is an array. Syntax: jQuery.isArray( object ) Parameters: The isArray() method accepts only one parameter that is mentioned above and described below: object : This parameter is the object to test whether or not it is an arra
    2 min read
  • jQuery inArray() method
    This inArray() Method in jQuery is used to search for a specific value in an array and return its index (or -1 if not found). SyntaxjQuery.inArray(val, arr [, Index])Parameters The inArray() method accepts three parameters that are described below: val: The value to search in an array.arr: Any array
    2 min read
  • Ember.js EmberArray filterBy() Method
    Ember.js is an open-source JavaScript framework used for developing large client-side web applications based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently,
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences