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

  • SEARCH
  • Home
  • 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 8489155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:44:55+00:00 2026-06-10T21:44:55+00:00

What is the most effective to map ranges using Arrays in JavaScript? I have

  • 0

What is the most effective to map ranges using Arrays in JavaScript?

I have this:

var def = [70,200,1000];

And an array of possible numbers, say:

var n = [23,45,74,120,240,800,1204,2000];

Now, how can I extract the closest value from n that matches the values from def?

In the example above, I would get [74,240,800]. I hope I make myself clear…

  • 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-10T21:44:56+00:00Added an answer on June 10, 2026 at 9:44 pm

    http://jsfiddle.net/8bY2M/

    var def = [70,200,1000];
    var n = [23,45,74,120,240,800,1204,2000];
    
    var result = [];
    
    for (var i = 0; i < def.length; i++){
        var val = def[i];
        for (var j = 0; j < n.length; j++){
            var nVal = n[j];                
            if (nVal > val){
                var closest = n[j-1] == undefined ? nVal : 
                    nVal - val > val - n[j-1] ? n[j-1] : nVal;
    
                result.push(closest);
                break;
            }    
        }
    }
    
    console.log(result); // outputs: [74, 240, 800] 
    

    Note that this will go with the larger value in the event of two matches. (i.e. if you are trying to match 70, and n contains 66 and 74, this will match it with 74)

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

Sidebar

Related Questions

What is the most effective and accurate way to geocode a UK postcode? Using
What's the cleanest, most effective way to validate decimal numbers in JavaScript? Bonus points
How do you truncate a PHP array in a most effective way? Should I
I have a side-effective operation securities |> Seq.map (fun x -> request.Append(securities,x)) What is
Possible Duplicate: Most effective way for float and double comparison I am new to
What crowdsourcing platforms are the most effective? and what measures have people used?
For something like this: What would be the most effective way to do this?
I have a series of li items and need the most effective way to
Possible Duplicate: Most effective way for float and double comparison In my program some
What is the most effective way to write this sql query in HQL select

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.