JS获取数组的最大值和最小值

需求:

高德轨迹返回的字符串为一连串的坐标点及海拔等数据,现在需要计算所有坐标点海拔最大落差;
也就是海拔的最大值和最小值的差值。
接口返回的 json 数据 result 是这样的:

{
    "id":0,
    "msg":"获取数据",
    "data":{
        "id":"2246",
        "record_id":"1",
        "startpoint":"29.012560221354168,120.47038384331597,gps,1490840902590,0.0,306.11",
        "endpoint":"29.011363,120.471702,gps,1490841850974,0.0,404.71",                    
        "pathline":"29.012560221354168,120.47038384331597,gps,1490840902606,0.0,306.11;29.01255425347222,120.47040256076389,gps,1490840905591,0.0,309.87;29.012554796006945,120.47040635850695,gps,1490840908583,0.0,312.38;29.01253824869792,120.47041314019097,gps,1490840911591,0.0,314.79;29.01252007378472,120.47042507595486,gps,1490840914608,0.0,314.65;29.01252007378472,120.47042507595486,gps,1490840917643,0.0,312.49;29.01251681857639,120.47042507595486,gps,1490840920586,0.39,311.22;29.012509223090277,120.47042236328124,gps,1490840923603,0.26,311.05;29.012479654947917,120.4704248046875,gps,1490840926583,1.05,311.11;29.01244384765625,120.47043891059027,gps,1490840929603,1.17,311.09;29.012423502604168,120.47045003255208,gps,1490840932589,1.06,311.03;29.01241021050347,120.4704652235243,gps,1490840935589,1.0,311.17;29.012403428819443,120.47046983506945,gps,1490840938608,0.92,311.55;29.012401801215276,120.47048963758681,gps,1490840941609,0.92,311.8;29.012410481770832,120.47051595052083,gps,1490840944610,0.9,311.75;29.01242513020833,120.47055284288194,gps,1490840947602,0.93,311.54;29.012442762586804,120.47056830512153,gps,1490840950592,0.87,311.39;29.012456868489583,120.47059326171875,gps,1490840953581,0.86,311.18;29.012467719184027,120.47059868706597,gps,1490840956594,0.62,311.0;29.012483452690972,120.4706165907118,gps,1490840959599,0.57,310.9;29.0125146484375,120.47065565321181,gps,1490840962600,1.01,310.85;29.01253200954861,120.4706787109375,gps,1490840965590,1.01,310.82;29.012550184461805,120.4707066514757,gps,1490840968591,0.94,310.82;29.01257378472222,120.47071831597222,gps,1490840971590,0.9,310.91;29.012590874565973,120.47073432074653,gps,1490840974587,0.88,310.97;29.01260064019097,120.47074245876736,gps,1490840977610,0.86,310.98;29.012616916232638,120.47076904296875,gps,1490840981592,0.63,310.95;29.01262234157986,120.47078342013889,gps,1490840984601,0.55,311.05;29.012636990017363,120.47081081814235,gps,1490840987587,0.79,311.31;29.012650282118056,120.4708251953125,gps,1490840990590,0.63,311.81;29.012664116753474,120.47084065755209,gps,1490840993587,0.67,312.35;29.01266899956597,120.47085693359375,gps,1490840996607,0.36,312.81;29.012671712239584,120.47087673611111,gps,1490840999585,0.32,313.29;29.01267822265625,120.47088785807291,gps,1490841002585,0.33,313.99;29.012677680121527,120.47089545355902,gps,1490841005585,0.14,314.49;29.012679036458334,120.47088758680556,gps,1490841008594,0.49,314.83;29.012674153645833,120.47088568793403,gps,1490841011591,0.06,315.02;",
        "travel_notes_title":"# 步行 # ",
        "travel_notes_img_count":"4",
        "distance":"1.913",
        "averagespeed":"7.26",
        "duration":"948",
        "audio_count":"0",
        "video_count":"0",
        "kacl":"66"
    }
}

result 数据中的 pathline 轨迹坐标点数据只截取了一部分, js 数据处理代码如下:

    var pathline = result.data.pathline;
    var info_arr = pathline.split(';');
    var altitude = [];// 海拔数据集合
    var point_arr = [];// 坐标点集合
    for(i=0;i<info_arr.length-1;i++){
        var arr = info_arr[i].split(',');
        point_arr[i] = [arr[1],arr[0]];
        altitude[i] = arr[5];
    }
    console.log(altitude);
    console.log(Math.max.apply(null, altitude));
    console.log(Math.min.apply(null, altitude));
    //计算海拔最大落差
    var high = Math.max.apply(null, altitude) - Math.min.apply(null, altitude);
    high = high.toFixed(2);

altitude 为包括全部坐标点的数据集合;
获取最大值 Math.max.apply(null, altitude) ,最小值使用 Math.min.apply(null, altitude)

输出的结果是:

["306.11", "309.87", "312.38", "314.79", "314.65", "312.49", "311.22", "311.05", "311.11", "311.09", "311.03", "311.17", "311.55", "311.8", "311.75", "311.54", "311.39", "311.18", "311.0", "310.9", "310.85", "310.82", "310.82", "310.91", "310.97", "310.98", "310.95", "311.05", "311.31", "311.81", "312.35", "312.81", "313.29", "313.99", "314.49", "314.83", "315.02"]
315.02
306.11

标题:JS获取数组的最大值和最小值

原文链接:https://beltxman.com/1612.html

若无特殊说明本站内容为 行星带 原创,未经同意请勿转载。

Scroll to top