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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:37:06+00:00 2026-05-29T22:37:06+00:00

I have a JSON object with an array of updates, where each update consists

  • 0

I have a JSON object with an array of “updates”, where each “update” consists of a timestamp and a message:

updates : {
    {
        timestamp : 1329505671,
        text : 'test1'
    },
    {
        timestamp : 1329505783,
        text : 'test2'
    }
}

Using jQuery, I parse these into DOM elements and insert them at the same level, in the order they were received. They are presorted based on my SQL query in the AJAX call.

Now, I have a recurring ajax call that checks for new updates. If an update is found, it needs to insert it into the DOM, in the appropriate position.

I cannot assume that these updates will always be the newest update in the list, so, I need a way to find which DOM update to insert the new update after.

I had planned on creating a Map<int, DOMUpdate> of sorts (updates[timestamp] = DOMUpdate in Javascript).

There are a couple flaws with this plan, though:

  1. I’d have to find/create a fast binary search algorithm for finding
    which timestamp to put the update after.
  2. I couldn’t have duplicate timestamps.

So my question is if anyone else has done something similar, and if so, how did you approach it? Please let me know if I’m being unclear in any way.

  • 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-29T22:37:07+00:00Added an answer on May 29, 2026 at 10:37 pm

    Just find the first element that has a timestamp that is greater than it and append it before that element. If none are found, it is the newest and should be appended at the start/end depending on the order you are posting them(newest first or last).

    var appended = false;
    $(collection).each(function(){
      var timestamp = $(this).data("timestamp"); // replace with your code
      if (timestamp < obj.timestamp) {
        $(this).before(buildNewElement(obj));
        appended = true;
        return false;
      }
    });
    if (!appended) {
      $(collection).last().after(buildNewElement(obj));
    }
    

    Obviously this is just a concept, you will need to modify it to fit your code/requirements.

    Edit:

    If you stored your data in an array of objects, and then added objects to that array as more were received, you could sort that array on the timestamp and then append the new objects based on the index after sorting. You would have to add a property to the newly received objects, and then remove that property as you loop through all of the objects and append only those that have that property.

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

Sidebar

Related Questions

Hi I have a JSON object that is a 2-dimentional array and I need
say I have the following json object; {'fname':'john', 'lname':'Locke'} and the following text boxes
I have an json Object like this. {'01/19/2012': Array[1],'02/19/2012': Array[7],'03/19/2012': Array[6]} Now i want
I have a json object array. I want to search the array and for
I have an array of json objects like so: [{a:b},{c:d},{e:f}] What is the best
I have a JSON array with ActiveRecord objects. These objects can be reconstructed using
I have a json object collection of geo locations that I build in the
I have created a json object from ruby with cobravsmongoose, however the attributes have
I have the following JSON object: { response: { status: 200 }, messages: [
I have a request that returns a JSON object with a single property which

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.