簡體   English   中英

如何根據 javascript 中一段時間內的值列表計算平均風速和風向

[英]How do I calculate average wind speed and direction from a list of values over a period in javascript

如何根據一段時間內的值列表計算平均風速和風向?

const directions = ["S","S","NE","SW","NNE","N"];
const speeds     = [11,9,9,7,18,10]

網上有很多資源可以計算 A 和 B 之間的平均值( javascriptMYSQLSQLServer ),但沒有什么是簡單易懂和易於使用的。

我認為這不是答案,但可以提供幫助


    function getAverageNumber(numbers){
        return +((numbers.reduce((a, b) => a + b, 0) / numbers.length).toFixed(0))
    }
    function getWindDirection(d){
        if(d===0 || d===360) return "N";
        if(d===90) return "E";
        if(d===180) return "S";
        if(d===270) return "W";
        if (d>0 && d<90) return "NE";
        if (d>90 && d<180) return "SE";
        if (d>180 && d<270) return "SW";
        if (d>270 && d<360) return "NW";
    } 
    function getWindDegrees(w){
        if(w==="N") return 0;
        if(w==="NE") return 45;
        if(w==="E") return 90;
        if(w==="SE") return 135;
        if(w==="S") return 180;
        if(w==="SW") return 225;
        if(w==="W") return 270;
        if(w==="NW") return 315;
    } 
    function getAverageWind(_directions, _speeds){
        const averageDirection = getAverageNumber(_directions.map(getWindDegrees));
        const averageSpeed = getAverageNumber(_speeds);
        return { speed: averageSpeed, direction: getWindDirection(averageDirection) }
    }

const result = getAverageWind(directions, speeds)

// i don't this answer is entirely accurate
console.log(`${result.speed} km/h ${result.direction}`)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM