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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:12:53+00:00 2026-05-29T20:12:53+00:00

If I have a javascript object that looks like the following: { 0: [0,50],

  • 0

If I have a javascript object that looks like the following:

{
  0: [0,50],
  1: [25,50],
  2: [148,60]
}

How can I insert a new row at an index dependant on the value?

E.g. if the row I wish to insert contains the data [122,65] I need to test the value ‘122’ versus the first value of all the the rows and then ‘splice’ the row in at the returned index. The second of each row value (50,50,50 in the example above) should not be taken into consideration. In this case the newd data should be inserted as row 2 since it is less than 148 and higher than 25… With the previous row 2 now becoming row 3.

I have been hunting high and low for an answer to this today and have stumbled across the following code which hints at the kind of functionality I am after, but does not quite work in this instance since if my understaning is correct, it only compares single numbers.

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)  {
   if(val1 > val2)   {
      return 1;
   }else if(val1 < val2)   {
      return -1;
   }
   return 0;
}

I don’t know if the above code will help anyone come up with a solution but I thought I would share it anyway. I am of course open to any javascript/jQuery method of achieving this.

Thank you.

  • 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-29T20:12:54+00:00Added an answer on May 29, 2026 at 8:12 pm

    This code could be improved greatly, but this really is a bandaid for a bad design.

    var myObj = {
      0: [0,50],
      1: [25,50],
      2: [148,60]
    }
    
    function makeArray( obj ) {
        var newArray = [];
        for( var x in obj ) {
            newArray.push(obj[x]);
        }
        return newArray;
    }
    
    function makeObject( newArray ) {
        var newObj = {};
        for(var i=0, n=newArray.length; i<n; i++) {
            newObj[i] = newArray[i];
        } 
        return newObj;
    }
    
    function custSortFnc(a,b) {
        return (a[0]==b[0]) ? a[1]>b[1] : (a[0] > b[0]) ? 1 : -1;
    }
    
    function addValue(val) {
        var newArray = makeArray( myObj );
        newArray.push( val );
        newArray.sort( custSortFnc );
        myObj = makeObject( newArray );
    }
    
    console.log(myObj);
    addValue([122,65]);
    
    for(var xx in myObj){
        console.log(xx + ":" + myObj[xx]);
    }
    

    The looping will be bad if you add tons of elements to this thing. I also did not really check the sort function, it could be wrong.

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

Sidebar

Related Questions

I have a JavaScript object that looks like the following: venue = function(map, dataSet)
I have an object in Javascript that looks like this function MyObject(){ this.name=; this.id=0;
I have a JavaScript object that looks something like this: var myTextOptions = {
I have a date string that looks like the following javascript format. I want
I have a Javascript object that (very simplified) looks like this if('undefined' !== typeof(listCtrls)){
I have a javascript object that looks somthing like this: var obj = {
I have a javascript hash (object?) we will call settings_hash that basically looks like
I have a Javascript object that basically represents a Row in an .NET GridView.
I'm creating my own JavaScript Array-like object and I have methods that call closures.
I have a Javascript object that requires 2 calls out to an external server

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.