How to write a function that returns array elements larger than a number in JavaScript ?
Last Updated :
15 Feb, 2023
Given an array arr and number n and the task is to write a function that returns an array whose items are larger than n.
Example:
Input: arr = [65, 16, 0, 6, 64, 1, 68]
n = 16
Output: [65, 64, 68]
Input: arr = [6, 46, 54, 6, 56, 54, 65, 4, 65]
n = 50
Output: [54, 56, 54, 65, 65]
To achieve this we have the following approaches :
Approach 1: Using Array.filter()
In this approach, we use the Array.filter method. At every iteration, we check if the value is greater than num or not.
Example:
Javascript
<script>
let returnLarger = (arr, num) => arr.filter(n => n > num);
console.log(returnLarger( [65, 16, 0, 6, 64, 1, 68], 16))
console.log(returnLarger([6, 46, 54, 6, 56, 54, 65, 4, 65], 50))
</script>
|
Output:
[65, 64, 68]
[54, 56, 54, 65, 65]
Approach 2: Using Array.reduce()
In this approach, we use the array.reduce method. Initialize the accumulator using an empty array and at every iteration check if the current value is greater than num then concatenate the current value with the accumulator and return it otherwise return the accumulator as it is.
Example:
Javascript
<script>
let returnLarger = (arr, num) => {
return arr.reduce((acc, curr)=>{
if (curr > num){
return acc.concat(curr)
} else {
return acc
}
}, [])
}
console.log(returnLarger( [65, 16, 0, 6, 64, 1, 68], 16))
console.log(returnLarger([6, 46, 54, 6, 56, 54, 65, 4, 65], 50))
</script>
|
Output:
[65, 64, 68]
[54, 56, 54, 65, 65]
Approach 3: Using the Array.map method:
In this approach, we iterate the array using the map method and at every iteration, we check if the current value is greater than num or not? If true, then we return the value as it. If false, then replace the item with an empty string (Or any falsy value). After that using the filter method remove the falsy values.
Javascript
<script>
let returnLarger = (arr, num) => {
return arr.map(v => v > num ? v : "" ).filter(Boolean)
}
console.log(returnLarger( [65, 16, 0, 6, 64, 1, 68], 16))
console.log(returnLarger([6, 46, 54, 6, 56, 54, 65, 4, 65], 50))
</script>
|
Output:
[65, 64, 68]
[54, 56, 54, 65, 65]
Approach 4: using the for loop:
In this approach, we create an empty array and iterate the given array using the for loop and at every iteration, we check the current value is greater than num or not. If true, then we push the value in the newly created array.
Javascript
<script>
let returnLarger = (arr, num) => {
let newArr = []
for (let i = 0; i < arr.length; i++){
if (arr[i] > num){
newArr.push(arr[i])
}
}
return newArr
}
console.log(returnLarger( [65, 16, 0, 6, 64, 1, 68], 16))
console.log(returnLarger([6, 46, 54, 6, 56, 54, 65, 4, 65], 50))
</script>
|
Output:
[65, 64, 68]
[54, 56, 54, 65, 65]
Approach 5: using the forEach loop:
In this approach, we create an empty array and iterate the given array using forEach loop and at every iteration, we perform expression with and operator where first operator checks the current value is greater than num or not if it return false new operation is not performed and if return true then it push the element to array.
Javascript
<script>
let returnLarger = (arr, num) => {
let newArr = []
arr.forEach( ele => (ele > num) && newArr.push(ele))
return newArr
}
console.log(returnLarger( [65, 16, 0, 6, 64, 1, 68], 16))
console.log(returnLarger([6, 46, 54, 6, 56, 54, 65, 4, 65], 50))
</script>
|
Output:
[ 65, 64, 68 ]
[ 54, 56, 54, 65, 65 ]
Similar Reads
How to get removed elements of a given array until the passed function returns true in JavaScript ?
The arrays in JavaScript have many methods which make many operations easy. In this article let us see different ways how to get the removed elements before the passed function returns something. Let us take a sorted array and the task is to remove all the elements less than the limiter value passed
4 min read
Javascript Program to Find the Largest Element in an Array
These are the following ways to find the largest element in JS array: Note: You must add a conditional check for checking whether the given array is empty or not, if the array is not empty then you can use the given below approaches to find out the largest element among them. 1. Using the Spread Ope
4 min read
How to get n largest elements from array in JavaScript ?
Here in this article, we will see how we can find the n maximum element from the array using Javascript. Example: Input: arr = [1, 2, 3, 4, 5, 6], n = 3;Output: 4, 5, 6Explanation: Here we will see the 3 largest elements in the given array are 4, 5, 6. Input: arr = [5, 76, 32, 98, 52, 57] n = 2;Outp
3 min read
How to move specified number of elements to the end of an array in JavaScript ?
The purpose of this article is to move some specified elements to the end of an array using JavaScript. Given an array of length say N, move some specified amount of elements say X to the end of the given array. Input: arr = [1, 2, 3, 4, 5]X = 2Output: The following array should be the output as the
3 min read
JavaScript Program to Find Largest Element in an Array
In this article, we are going to learn about the largest element in an array in JavaScript. The largest element in an array refers to the value that holds the greatest numerical or lexicographic (string) order among all elements present in the array. Example: Input : [10, 15, 38, 20, 13];Output: 38H
4 min read
How to test a value x against predicate function and returns fn(x) or x in JavaScript ?
In this article, we are given a value x, and the task is to check the value against the predicate function. If the value satisfies the condition or predicate return fn(x) else return x. Predicate functions are the function that takes one item as input and returns true or false based on the whether i
3 min read
JavaScript Program to Find kth Largest/Smallest Element in an Array
JavaScript allows us to find kth largest/smallest element in an array. We are given an array containing some elements, we have to find kth smallest/largest element from the array where k is a number greater than zero and less than equal to the total number of elements present in the array. There are
5 min read
How to find the Total Number of Elements in an Array in TypeScript ?
In TypeScript, arrays are a common data structure used to store collections of elements. You can store multiple elements of different or the same data type inside them by explicitly typing. The below methods can be used to accomplish this task: Table of Content Using the length property Using the fo
3 min read
How to Add the Numbers in a JavaScript Array?
Adding the numbers in a JavaScript array is a common operation, particularly in tasks involving data aggregation or mathematical calculations. This process involves iterating through the array and summing its elements. JavaScript provides multiple methods to achieve this, ranging from traditional lo
2 min read
Print all Even Numbers in a Range in JavaScript Array
We have to find all even numbers within a given range. To solve this question we are given the range(start, end) in which we have to find the answer. There are several ways to print all the even numbers in a range in an array using JavaScript which are as follows: Table of Content Using for Loop in
3 min read