timo
timo

Reputation: 2169

How do i convert all numbers in an array to their corresponding month?

I've got an array: months that is populated by numbers. I'm trying to overwrite the content of this array, exchanging the numbers for their corresponding months. So 1 becomes Januari and 2 becomes Februari and so forth. The numbers are always in correct order, but may contain several loops of an entire year and sometimes don't start at Januari aka 1.

I've tried just replacing the index with their corresponding months, but this is not a clean solution, also, it only replaces the first entry it finds.

months[months.indexOf(1)] = 'Januari';
months[months.indexOf(2)] = 'Februari';
months[months.indexOf(3)] = 'Maart';
months[months.indexOf(4)] = 'April';
months[months.indexOf(5)] = 'Mei';
months[months.indexOf(6)] = 'Juni';
months[months.indexOf(7)] = 'Juli';
months[months.indexOf(8)] = 'Augustus';
months[months.indexOf(9)] = 'September';
months[months.indexOf(10)] = 'Oktober';
months[months.indexOf(11)] = 'November';
months[months.indexOf(12)] = 'December';

What would be a clean sollution to replace all numbers in an array with their corresponding month?

http://jsfiddle.net/es4rQ/

Upvotes: 0

Views: 361

Answers (4)

lennon626
lennon626

Reputation: 124

The slice method will let you delete an element or replace it with something else. You would have to do this 12 times. There's probably a better solution, but this is my 2 cents.

var months= [1,2,3,4,5,6,7,8,9,10,11,12];
var theMonths = ["Jan","Feb","March","April","May","June","July","Aug",
"Septem","Oct","Nov","Dec"];

months.splice(months.indexOf(1),1,theMonths[0]);

Upvotes: 1

Ron
Ron

Reputation: 6735

Simple solution is here:

var months = [1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12];
var results = []
var month = []
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";


months.forEach(function(n) {
    results.push (month[n-1])
});

$("#months").html(results.join(' '));

Upvotes: 1

Tuanderful
Tuanderful

Reputation: 1319

A map might come in handy here:

JSFiddle: http://jsfiddle.net/B2k5A/1/

var monthsArray = [1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12];

var MONTHS = {
    1: 'Januari',
    2: 'Februari',
    3: 'Maart',
    4: 'April',
    5: 'Mei',
    6: 'Juni',
    7: 'Juli',
    8: 'Augustus',
    9: 'September',
    10: 'Oktober',
    11: 'November',
    12: 'December'
}

output = monthsArray.map(function(i){
   return MONTHS[i];
});

$("#months").html(output);

I created an object that stores the names of the months. The map will return an array by looping over your input, monthsArray, and for each item in monthsArray it will return the corresponding name of the month as defined in the MONTHS object.

Upvotes: 1

Jon
Jon

Reputation: 437336

A simple loop with lookup-and-replace will do:

var monthNames = ['Januari', 'Februari', ... ];
for (var i = 0; i < months.length; ++i) {
    months[i] = monthNames[months[i] - 1];
}

Upvotes: 1

Related Questions