Unit conversion with javascript Array.map()
Apr 11, 2016 • 3 Minute Read
This tutorial will demonstrate how to convert units of measurement with the javascript Array.map() method. The below examples are specific to converting a group of measurements from one unit to another with javascript. For example, if you need to convert a group of distances that were measured in miles to kilometers or a product weighed in ounces to pounds or maybe you need to convert a group of percentages to fractions. An array of these measurements can conveniently and quickly get converted with javascript’s Array.map() method.
The map() method is called as a prototype method of an array. The function passed to array.map() runs on every element in that array, and then returns a new array with the returned values.
The following examples convert distance, temperature, weight, and time to demonstrate some scenarios for when and how to use the Array.map() method.
- Convert Meters to Centimeters
- Convert Celsius to Fahrenheit
- Convert Kilograms to Grams
- Convert Years to Days
Let’s say we have an array of measurements in meters, but what we really need is an array of those measurements in centimeters. To convert from Meters to Centimeters multiply total meters by 100
function meters_to_centimeters( distance ) {
var centimeters = distance * 100;
return centimeters;
}
var meters_array = [2, 1.5, 14, 4.2, 8];
var centimeters_array = meters_array.map(meters_to_centimeters);
console.table([meters_array,centimeters_array]);
//meters: [2, 1.5, 14, 4.2, 8];
//centimeters: [200, 150, 1400, 420, 800]
Let’s take a look at an example using the map() method to convert an array of temperatures in Celsius to an array of temperatures in Fahrenheit. To convert temperatures in degrees Celsius to Fahrenheit, multiply by 1.8 and then add 32.
function celsius_to_fahrenheit( temperature ) {
var fahrenheit = (temperature * 1.8) + 32;
return fahrenheit;
}
var temperatures_in_celsius = [10, 16, 20, 24, 32];
var temperatures_in_fahrenheit = temperatures_in_celsius.map(celsius_to_fahrenheit);
console.table([temperatures_in_celsius,temperatures_in_fahrenheit]);
//celsius: [10, 16, 20, 24, 32];
//fahrenheit: [50, 60.8, 68, 75.2, 89.6];
Let’s take a look at an example using the map() method to convert a array of weights in kilograms to an array in grams. To convert weight from Kilograms to grams, multiply by 1000
function kilograms_to_grams( weight ) {
var grams = weight * 1000;
return grams;
}
var weights_in_kilograms = [12, 6, 2, 14, 7];
var weights_in_grams = weights_in_kilograms.map(kilograms_to_grams);
console.table([weights_in_kilograms, weights_in_grams]);
//kilograms: [12, 6, 2, 14, 7];
//grams: [12000, 6000, 2000, 14000, 7000];
Lastly, let’s use the javascript map() method to convert an array of time in years to an array in days. To convert years to days, multiply years by 365 days
function years_to_days( time ) {
var days = time * 365;
return days;
}
var years_array = [12, 21, 31, 54, 72];
var days_array = years_array.map(years_to_days);
console.table([years_array, days_array]);
//years: [12, 21, 31, 54, 72];
//days: [4380, 7665, 11315, 19710, 26280];
To summarize, javascripts Array.map() method is useful for converting a group of measurements with javascript. Whether you are looking to convert a group of distances, temperatures, weights, times or other units, Array.map() can handle the job. Array.map() goes much deeper and can handle many more use cases. Array.map() also has parameters passed in with the callback function including currentValue, index, and the array that .map() was called from. While the above examples map over an array of numbers, .map() can also be used with strings, objects, and other types stored within the array.
For a more in depth explanation of the Array.map() javascript method, checkout the Mozilla Developer Network map() method documentation.
MDN Array.prototype.map()