Javascript 如何在js中使用Map、REDUCT和filter将华氏温度转换为摄氏温度

Javascript 如何在js中使用Map、REDUCT和filter将华氏温度转换为摄氏温度,javascript,arrays,dictionary,Javascript,Arrays,Dictionary,我有一个温度数组,我必须将它们转换为摄氏度,有人告诉我需要在javascript中使用map/reduce。我已经看了文档,但似乎不知道怎么做。 这是我的阵列: var fahrenheit = [0, 32, 45, 50, 75, 80, 99, 120]; 试着这样用, var fahrenheit = [0, 32, 45, 50, 75, 80, 99, 120]; var celcius = fahrenheit.map(v => ((v - 32) * (5/9)).toF

我有一个温度数组,我必须将它们转换为摄氏度,有人告诉我需要在javascript中使用map/reduce。我已经看了文档,但似乎不知道怎么做。 这是我的阵列:

var fahrenheit = [0, 32, 45, 50, 75, 80, 99, 120];
试着这样用,

var fahrenheit = [0, 32, 45, 50, 75, 80, 99, 120];
var celcius = fahrenheit.map(v => ((v - 32) * (5/9)).toFixed(1));
//If you do not want the decimal points then write,
//   fahrenheit.map(v => ((v - 32) * (5/9)) | 0);

console.log(celcius); //["-17.8", "0.0", "7.2", "10.0", "23.9", "26.7", "37.2", "48.9"]
将F转换为C的公式为

C=((F-32)*(5/9))


我想这就行了

var fahrenheit = [0, 32, 45, 50, 75, 80, 99, 120],
       celcius = fahrenheit.map(f => (f-32)/1.8);
这应该行得通

  var celcius = fahrenheit.map(function(elem) {
        return Math.round((elem - 32) * 5 / 9);
    });
或者在ES6中

fahrenheit.map(elem => Math.round((elem - 32) * 5 / 9));
你得到了什么

celcius //  [-18, 0, 7, 10, 24, 27, 37, 49]

如果你解释你的代码是如何工作的,而不是只提供一个代码的答案,那会更有帮助。什么文档?有,这可能有点难,但值得一读。还有MDN,它是一个公共维基,提供非常有用的建议,虽然它不是权威,所以它在技术上并不总是正确的,但它仍然非常好。