Ankit
Ankit

Reputation: 100

I have an array and I want to get new array which value is greaterthan 60

I have an array and I want to get new array of the elements whose valus are greater than 60

This is an array.

const ages = [23,62,45,234,2,62,234,62,34];

I want to get new array from this array which value has greate than 60.

Expected Result: [ 62, 234, 62, 234, 62 ]

Upvotes: 1

Views: 51

Answers (2)

Rohit Sharma
Rohit Sharma

Reputation: 3334

It is an another solution. Just check the values that are greater than 60 and push them into the new array.

var greaterages = [];
const ages = [23,62,45,234,2,62,234,62,34];
for (var i = ages.length - 1; i >= 0; i--) {
    if(ages[i] > 60) {
        greaterages.push(ages[i]);
    }
}
console.log(greaterages);

Upvotes: 0

Ankit Parmar
Ankit Parmar

Reputation: 790

This code will help. Use a simple filter array and store results in a new constant.

const old = ages.filter(age => age >= 60);

Upvotes: 5

Related Questions