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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:24:39+00:00 2026-06-09T19:24:39+00:00

I have this Javascript below, and I know that there is probably a better

  • 0

I have this Javascript below, and I know that there is probably a better way of doing what I have done, so if you would like to share a better way, please do and I will definitely use it.

var Apeople = ["bob", "joe", "jane", "mike", "henry", "alex"];
var Abdays = ["08/20", "01/23", "04/19", "08/16", "01/08", "04/02"];

Apeople and Abdays line up with each other, so Janes birthday is 04/19.

I know I’m not supposed to use eval() so if you know of a better way please tell me.

var mnths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
for(i=0; i<mnths.length; i++){
   eval("var "+mnths[i]+" = []");
}

for(i=0; i<Abdays.length; i++){
   var ab = Abdays[i];
   if(ab.substring(0,1) == "0"){
      var mn = ab.substring(1,2); //month number without 0 before it
   } else {
      var mn = ab.substring(0,2); //month number if 11 or 12
   }
   eval(""+mnths[mn-1]+".push(i)");
}

Now I have January=[1,4] April=[2,5] August=[0,3]. I did this so I can find out whos name goes with what birthday.

For example Apeople[January[0]] would be joe and Abdays[January[0]] would be 01/23.

What I need to do is put each months entries in order by which comes first going by the birthdays. Abdays[January[0]] is 01/23 but Abdays[January[1]] is 01/08 so they should be swapped.

As I am typing this I am thinking that I probably should have used JSON.

So what I need help with is putting the birthdays in order while still being able to tell which birthday goes to which name, and at the same time possibly finding a better way to write this code.

  • 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-09T19:24:41+00:00Added an answer on June 9, 2026 at 7:24 pm

    You can sort theAbdays array like this.

    Abdays = Abdays.sort(function(a, b){
       var d1 = new Date(a + " 2010");
       var d2 = new Date(b + " 2010");
       return d1 > d2;
    });
    

    I randomly selected 2010. You will have to modify this, add more conditions if you need and write code to sort Apeople according to sorted Abdays .

    UPDATE:

    Quickly written. You can find better ways, but hope this helps

    var map = {};
    for(var i=0 ; i < Apeople.length; i++){
       map[Abdays[i]] = Apeople[i];
    }
    
    Abdays = Abdays.sort(function(a, b){      
       var d1 = new Date(a + " 2010");
       var d2 = new Date(b + " 2010");
       return d1 > d2;
    });
    
    for(var i=0 ; i < Apeople.length; i++){
       Apeople[i] = map[Abdays[i]];
    }
    
    console.log(Apeople, Abdays);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this javascript code below that uses jquery, it is suppoed to be
I have this Javascript function that sends username and password to php file through
I have this JavaScript code that creates a table, and puts in the rows
I have a String like below: <script language=JavaScript type=text/javascript src=http://dns.net/adj/myhost.com/index;size=5x10;zipc=12345;myzon=north_west;|en;tile=10;ord=7jkllk456?></script> I want to access
I have come to know that from IE 5, there is a strange behaviour
This is a general question. Using an example below, suppose that I have a
I know that there are several ways to define a function in JavaScript. Two
I have this Javascript/JQuery code: <script type=text/javascript src=http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js></script> <script type=text/javascript> name = $(#name).val(); alert(Name:
I have this Javascript: <script language=javascript type=text/javascript> $().ready(function() { $('#ex2').jqm({ajax: 'view.php?id=<?=$objResult[id];?>', trigger: 'a.ex2trigger'}); });
I have this Javascript code I inherited from another developer. I am very new

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.