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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:13:12+00:00 2026-06-09T17:13:12+00:00

I have an array of data get from the server(ordered by date): [ {date:2012-8,

  • 0

I have an array of data get from the server(ordered by date):

[ {date:"2012-8", name:"Tokyo"}, {date:"2012-3", name:"Beijing"}, {date:"2011-10", name:"New York"} ]

I’d like to :

  1. get the name of the first element whose date is in a given year, for example, given 2012, I need Tokyo
  2. get the year of a given name
  3. change the date of a name

which data structure should I use to make this effective ?

because the array could be large, I prefer not to loop the array to find something

  • 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-09T17:13:13+00:00Added an answer on June 9, 2026 at 5:13 pm

    Since it appears that the data is probably already sorted by descending date you could use a binary search on that data to avoid performing a full linear scan.

    To handle the unstated requirement that changing the date will then change the ordering, you would need to perform two searches, which as above could be binary searches. Having found the current index, and the index where it’s supposed to be, you can use two calls to Array.splice() to move the element from one place in the array to another.

    To handle searches by name, and assuming that each name is unique, you should create a secondary structure that maps from names to elements:

    var map = {};
    for (var i = 0, n = array.length; i < n; ++i) {
        var name = array[i].name;
        map[name] = array[i];
    }
    

    You can then use the map array to directly address requirements 2 and 3.

    Because the map elements are actually just references to the array elements, changes to those elements will happen in both.

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

Sidebar

Related Questions

Previously i have used restful wcf webservices to get the data from server.But now
I have some JSON data that I get from a server. In my JavaScript,
I have three dimensional array of data that is generated from web server logs
I have some array data returned from an ajax call consisting of an image
I have a problem related to an array data which fetched from an external
I have a TCP socket client receiving messages (data) from a server. messages are
I have an app where I fetch data from server(json) in the form of
I have some code that I'm using to get data from a network socket.
I have to handle large amount of data retrieved from server using ajax and
I have method List<Foo> getFoos () which gets the data from remote server and

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.