Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8420269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:50:53+00:00 2026-06-10T02:50:53+00:00

I have a JavaScript array and a variable like so; var a = [0,

  • 0

I have a JavaScript array and a variable like so;

var a = [0, 1200, 3260, 9430, 13220],
    b = 4500;

What would be the smartest way to select the largest value in the array that’s still smaller than or equal to the variable?

In this example, I’d need to select 3260.

I could do something like this;

$.each(a, function(i){
    if(a[i] <= b && a[i+1] > b){
        var c = a[i];
        return false;
    }
});

But I’m thinking that might not work if the selected array value is the last one. Not to mention, to me it looks like a lot of code for something rather simple.

Is there a smarter/less verbose way of achieving what I’m after?

(and yes, I know I shouldn’t have used a jQuery loop for that but I’m lazy when typing examples)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:50:54+00:00Added an answer on June 10, 2026 at 2:50 am

    Another way you could to it is through a combination of array filtering and apply(), which I think is a very readable approach.
    The call to filter() just returns an array of elements in a which don’t satisfy the predicate function and then apply() calls Math.max with each element as an argument.

    var a = [1, 2, 3, 4, 5];
    var b = 4;
    var result = Math.max.apply(Math, a.filter(function(x){return x <= b}));
    

    Result will be equal to 4.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have javascript code some thing like this -- var count=3; var pl=new Array(count);
If I have a javascript object/assoc. array defined like this: function somefunction(options) { var
I have a variable length array of strings declared in javascript that contains Dungeons
I have a javascript array: var exclude = [Santorum,Obama,Romney,Gingrich]; I have html links: <a
for example: I have created one javascript property array model with some properties like
I have an array in Javascript: var array = new array(); array[0] = apples;
I have an array of JavaScript objects: var objs = [ { first_nom: 'Laszlo',
I have the following in javascript: var entriesString = ''; $$('select[id=shopId]').each(function(elem, i){ shops[i] =
I have a JAVASCRIPT array that looks like this: postarray['min_price'] = 120000; postarray['max_price'] =
I have a javascript object that looks somthing like this: var obj = {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.