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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:50:25+00:00 2026-05-16T10:50:25+00:00

Let’s say I have an array var test = new Array() the values in

  • 0

Let’s say I have an array

var test = new Array()

the values in test are 3,6,9,11,20

if I then have a variable

var id = 5

how can I insert 5 between 3 and 6 in the array?
or do I just insert it wherever and then sort the array?

Thanks in advance.

edit:

I have the following code:

function gup( filter )
{
  filter = filter.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+filter+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

var queryString = gup("SelectedID");


var hrefs = new Array();
$('.table404').children().children().each(function(){
var link = ($(this).find('a').attr('href'));
var startIndex = link.indexOf(",'");
var endIndex = link.indexOf("');");
if ( startIndex >= 0 && endIndex >= 0 ) {
var linkID = link.substring( startIndex+2, endIndex );
hrefs.push(linkID);
hrefs.push(queryString);
hrefs.sort()
}
alert(hrefs);
});

for each item inserted into the array I get an alert with the ID but for every item I get one 1 (the current queryString value), so the last pop up looks something like
1,1,1,1,1,2,4,6,7,8

Why do I get a new pop up for every item inserted into the array? I get the querystring value once for every other item inserted into the array. What do I have to do to get one pop up with the complete array?

  • 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-05-16T10:50:25+00:00Added an answer on May 16, 2026 at 10:50 am

    You can use a binary searach to find an insertion point, if you array is large enough:
    Below is a quick code with tests. (Warning: not thoroughly tested). Also the array has to be a sorted array.
    Once you have an insertion point, just use the Array.splice function to insert at that index.

    /**
     * Find insertion point for a value val, as specified by the comparator 
     * (a function)
     * @param sortedArr The sorted array
     * @param val The value for which to find an insertion point (index) in the array
     * @param comparator The comparator function to compare two values
     */
    function findInsertionPoint(sortedArr, val, comparator) {   
       var low = 0, high = sortedArr.length;
       var mid = -1, c = 0;
       while(low < high)   {
          mid = parseInt((low + high)/2);
          c = comparator(sortedArr[mid], val);
          if(c < 0)   {
             low = mid + 1;
          }else if(c > 0) {
             high = mid;
          }else {
             return mid;
          }
          //alert("mid=" + mid + ", c=" + c + ", low=" + low + ", high=" + high);
       }
       return low;
    }
    
    /**
     * A simple number comparator
     */
    function numComparator(val1, val2)  {
       // Suggested b @James
       return val1 - val2;
    }
    
    // TESTS --------------------------------
    
    var arr = [0,1,3,6,9,11,20];
    var idx = findInsertionPoint(arr, 2, numComparator);
    arr.splice(idx, 0, 2);
    alert(arr); // will alert [0,1,2,3,6,9,11,20]
    
    var arr2 = [0,1,3,6,9,11,20];
    var idx2 = findInsertionPoint(arr2, -1, numComparator);
    arr2.splice(idx2, 0, -1);
    alert(arr2); // will alert [-1,0,1,3,6,9,11,20]
    

    If you have different objects, the only thing you need to do is provide appropriate comparator function.

    Or if the array is really small and if you are especially lazy today, you can just do:

    test.push(2).sort();

    test.push(2); test.sort();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me explain best with an example. Say you have node class that can
Let's say I have a table with a Color column. Color can have various
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let's say I have a javascript array with a bunch of elements (anywhere from
Let's say I have a dataset, which can be neatly classified using weka's J48
Let's say I have a list of business objects listed in the array @businesses
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have a method in java, which looks up a user in
Let's say that I have a SQLite database that I create in a separate

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.