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 8105121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:05:00+00:00 2026-06-06T00:05:00+00:00

I am looking for a function that would search for the closet 2 elements

  • 0

I am looking for a function that would search for the closet 2 elements out of an array of object based on a known object value. The function would return the indexes for the 2 closest elements or a single index if there is a direct match. it would search by the p variable in each element.

(It is safe to assume the p variable will not appear more than once)

var orbit = [ // p is percent
    { p:   0, x:   0, y:   0, z: 1.2 }  
    { p:  30, x:  30, y: 100, z: 0.5 }  
    { p:  45, x: 100, y:  30, z: 0.7 }  
    { p:  75, x:  60, y:   0, z: 1.0 }  
    { p: 100, x:   0, y:   0, z: 1.2 }  
];

function ValueToIndexes (value) {
    return [close1, close2];
};

if the value was 60 it would return [2,3]
if the value was 30 it would return [1]

  • 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-06T00:05:01+00:00Added an answer on June 6, 2026 at 12:05 am
    var ValueToIndices = function (orbit, value) {
    
        var 
            /* storage for distances */
            distances = [],
    
            /* sort helper */ 
            sortByDistance = function (a, b) {
                return a.d - b.d;
            };
    
        /* iterate over orbit */
        for (var i = 0; i < orbit.length; i++) {
    
            /* a direct match returns immediately */
            if (orbit[i].p === value) {
                return [i];
            }
    
            /* else collect all distances to the value */
            distances.push({
                i: i,
                d: Math.abs(orbit[i].p - value)
            });
        }
    
        /* sort the distances */
        distances.sort(sortByDistance);
    
        /* return the indices of the first two */
        return [distances[0].i, distances[1].i];
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for a function that would be the alphabetic equivalent of is_numeric.
Is there a Vim plugin, script, or function out there that allows Vim search
I've been looking for a class in MPL that will create a function object
Greetings! I'm looking for a way to search a collection for the object that
I'm looking for a function that calculates years from a date in format: 0000-00-00.
I am looking for a function that will tell me, for a list of
I am Looking for a Function that insert Any form values to mysql ,
Similar in concept to Math.Abs() - I'm looking for a function that when given
I'm looking for a hash function that: Hashes textual strings well (e.g. few collisions)
I'm looking to create a PHP function that can trim each line in a

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.