- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Filtering array within a limit JavaScript
We are required to write a JavaScript function that takes in an array of numbers as the first argument and an upper limit and lower limit number as second and third argument respectively. Our function should filter the array and return a new array that contains elements between the range specified by the upper and lower limit (including the limits)
Example
const array = [18, 23, 20, 17, 21, 18, 22, 19, 18, 20]; const lower = 18; const upper = 20; const filterByLimits = (arr = [], upper, lower) => { let res = []; res = arr.filter(el => { return el >= lower && el <= upper; }); return res; }; console.log(filterByLimits(array, upper, lower));
Output
And the output in the console will be −
[ 18, 20, 18, 19, 18, 20 ]
- Related Articles
- JavaScript - filtering array with an array
- Filtering array of objects in JavaScript
- Filtering out primes from an array - JavaScript
- Chunking array within array in JavaScript
- Array filtering using first string letter in JavaScript
- Filtering array to contain palindrome elements in JavaScript
- Find average of each array within an array JavaScript
- Filtering of JavaScript object
- Find average of each array within an array in JavaScript
- Checking if all array values are smaller than a limit using JavaScript
- Finding confusing number within an array in JavaScript
- Number of vowels within an array in JavaScript
- Sum identical elements within one array in JavaScript
- Counting possible APs within an array in JavaScript
- How to check whether multiple values exist within a JavaScript array

Advertisements