JavaScript Array.filter Example and Usage – Letstacle

JavaScript Array.filter Example and Usage – Letstacle 0 HomeLive Programming HelpComputer Science Homework HelpPython Homework HelpPython Homework SolvedJava Homework HelpJava Homework solvedC++ Homework HelpR Homework HelpPHP Homework HelpHTML Homework HelpJavaScript Homework HelpSQL Homework HelpDo My Programming HomeworkAndroid Assignment HelpAboutHow it WorksCareersArticlesJavaJavaScriptPythonC++hackerrank solutionsHTMLMathsDatabase AcademyPython Tutor ReviewsRecent ReviewsLeave a Review PaymentFAQs Contact / Get Help JavaScript Array.filter Example and Usage July 30, 2021 0 Comments Searching for JavaScript array.filter example?In this JavaScript tutorial, We will go through various examples and see the use of array.filter() method in action.Usage: Array.filter() JavaScript MethodThe filter method creates a new array containing all the elements which pass(evaluates to true) the test condition provided in the function.Note: If no value evaluates to true condition then an empty array is created.Syntax of Array.filterJavaScript array.filter syntax is as below-// arrow function filter((element) => { … }) filter((element,index) => { … }) filter((element,index,array) => { … }) // Callback function filter(callbackFn) filter(callbackFn, thisArg) // 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) Example 1: Filter a Number ArrayTask description -Filter the positive and negative numbers in Javascript using the Array.filter method. //given array conating both positive & negative numbers const numbers = [1,-5,2,0,-1,8,-3,10,9,-8]; //get the positive numbers in a new array const positiveNum = numbers.filter(num => num > 0); console.log(positiveNum); // show them on console //get the negative numbers in a new array const negativeNum = numbers.filter(num => num < 0); console.log(negativeNum); // show them on console Example 2: Filter a String ArrayTask description- Get all the consonants from an array of alphabets. Use JavaScript Array.filter to complete the task. //alphabet array const alphabets = ['a','b','c','d','e','f','g','h','i','j', 'k','l','m','n','o','p','q','r','s','t', 'u','v','w','x','y','z']; //get the consonents const conconents = alphabets.filter(alpha => alpha!=’a’ && alpha!=’e’ && alpha!=’i’ && alpha!=’o’ && alpha!=’u’); //show them on console console.log(conconents); Example 3: Filter an Array of ObjectsTask description- Get the detail of the person who can vote, the voting criteria age should be equal to 18 or greater. //array of objects containg the detail of people const people= [ { id: 1, name: “Jack”, age: 18 }, { id: 2, name: “Jessy”, age: 20, }, { id: 3, name: “Mark”, age: 16, }, { id: 4, name: “Vishal”, age: 22, } ]; //get the array with only who is eligable to vote using JS filter method const pplCanVote = people.filter((people, index, arr) => { return arr[index].age >= 18; }); //show it on console. console.log(pplCanVote); Finally, We hope you understood JavaScript Array.filter example doesn’t forget to follow us on Facebook and LinkedIn. You can also subscribe to our weekly FeedShare Article: July 30, 2021Node JS Post Request | Working HTTP Post Request July 31, 2021JavaScript to Lowercase | 2 Ways to Change String Leave a Reply Cancel replyLog In Related Articles September 17, 2021JavaScript | Function not defined September 14, 2021JavaScript | Array Length September 8, 2021JavaScript | Get Last Element of Array September 7, 2021JavaScript Clear Array | 3 Ways to Empty Array August 22, 2021JavaScript Square Root | Usage, Syntax & Program March 17, 2022How to Run JavaScript in Visual Studio Code? CategoriesBlog C++ Commerce Help Database Do my Economics Help hackerrank solutions HTML Java JavaScript Java solved Maths Maths Help Node JS Online Tutor Programming Help Python Python solved Quiz Uncategorized Recent Comments On LetstacleJimenez on Computer Science Homework Help | Computer Science HelpLetstacle Team on Python Homework Help | Help with Python HomeworkLetstacle Team on Python Homework Help | Help with Python HomeworkLetstacle Team on Python Homework Help | Help with Python HomeworkLetstacle Team on Python Homework Help | Help with Python HomeworkImler Miller on Python Homework Help | Help with Python HomeworkFeatured posts April 28, 2022Visual Basic Homework Help | VB.net Coding Help April 28, 2022Computer Science Homework Help | Computer Science Help February 15, 2022Do my Java Homework | Get Java Homework Help OnlineCategoriesBlog C++ Commerce Help Database Do my Economics Help hackerrank solutions HTML Java JavaScript Java solved Maths Maths Help Node JS Online Tutor Programming Help Python Python solved Quiz UncategorizedQuick LinksMake PaymentLeave a ReviewStoreTop Rated Rated 4.9 out of 5 Made with <3 creative team @ 2021 Letstacle.comTerms of Use | Disclaimer | Privacy Policy

Leave a comment

Your email address will not be published.