使用Javascript数组的一点帮助

使用Javascript数组的一点帮助,javascript,jquery,api,Javascript,Jquery,Api,我在使用API数组时遇到了一些问题。基本上,我只想显示来自API数组的第一个信息对象。我一直在玩弄Slice()和substr()函数,但都没能让它正常工作。我是说,我已经做了一件事来表达你的愿望 HTML: <h3>Wind Speed:</h3><div class='speed'></div> <br> <h3>Wind Direction:</h3><div class='thug'><

我在使用API数组时遇到了一些问题。基本上,我只想显示来自API数组的第一个信息对象。我一直在玩弄Slice()和substr()函数,但都没能让它正常工作。我是说,我已经做了一件事来表达你的愿望

HTML:

<h3>Wind Speed:</h3><div class='speed'></div> <br>
<h3>Wind Direction:</h3><div class='thug'></div><br>
<h3>Wave Height:</h3><div class='test'></div><br>
<h3>Ignore:</h3><div class='wave'></div><br>
var url = 'http://magicseaweed.com/api/CP86f5quQqmB1fpW2S3bZVUCS8j1WpUF/forecast/?spot_id=1323'

$.ajax({
    dataType: "jsonp",
    url: url
}).done(function(data) {
    console.log(data);
    sum = 0;

   //---Wind Speed---------------------------------- 
     $.each(data, function(){
        sum += this.wind.speed;
    });
    $('.speed').html(sum / data.length + data[0].wind.unit);



     //---Ignore---------------------------------- 
     $.each(data, function(){
        sum += this.swell.maxBreakingHeight;
    });
    $('.wave').html(sum);


     //---Wave Height---------------------------------- 

    $.each(data, function(){
        $('.test').append('<p>' + this.swell.maxBreakingHeight + '</p>'); 
    });


     //---Wind Direction---------------------------------- 

    $.each(data, function(){
        sum += this.wind.compassDirection ;
    });

    var numbers = sum;
     $('.thug').append('<p>' + numbers.slice( 3,4) + '</p>');
});
风速:
风向:
波高:
忽略:
JS:

<h3>Wind Speed:</h3><div class='speed'></div> <br>
<h3>Wind Direction:</h3><div class='thug'></div><br>
<h3>Wave Height:</h3><div class='test'></div><br>
<h3>Ignore:</h3><div class='wave'></div><br>
var url = 'http://magicseaweed.com/api/CP86f5quQqmB1fpW2S3bZVUCS8j1WpUF/forecast/?spot_id=1323'

$.ajax({
    dataType: "jsonp",
    url: url
}).done(function(data) {
    console.log(data);
    sum = 0;

   //---Wind Speed---------------------------------- 
     $.each(data, function(){
        sum += this.wind.speed;
    });
    $('.speed').html(sum / data.length + data[0].wind.unit);



     //---Ignore---------------------------------- 
     $.each(data, function(){
        sum += this.swell.maxBreakingHeight;
    });
    $('.wave').html(sum);


     //---Wave Height---------------------------------- 

    $.each(data, function(){
        $('.test').append('<p>' + this.swell.maxBreakingHeight + '</p>'); 
    });


     //---Wind Direction---------------------------------- 

    $.each(data, function(){
        sum += this.wind.compassDirection ;
    });

    var numbers = sum;
     $('.thug').append('<p>' + numbers.slice( 3,4) + '</p>');
});
var url='1〕http://magicseaweed.com/api/CP86f5quQqmB1fpW2S3bZVUCS8j1WpUF/forecast/?spot_id=1323'
$.ajax({
数据类型:“jsonp”,
url:url
}).完成(功能(数据){
控制台日志(数据);
总和=0;
//---风速------------------
$.each(数据,函数(){
总和+=该风速;
});
$('.speed').html(sum/data.length+data[0].wind.unit);
//---忽略-------------------
$.each(数据,函数(){
sum+=this.swell.maxBreakingHeight;
});
$('.wave').html(总和);
//---波高------------------
$.each(数据,函数(){
$('.test').append(''+this.swell.maxBreakingHeight+'

'); }); //---风向------------------- $.each(数据,函数(){ sum+=this.wind.compassDirection; }); var数=总和; $('.thug').append(''+numbers.slice(3,4)+'

'); });
在本例中,我只希望它显示第一个数字,在本例中是“4”。如果有人能帮助我,我将不胜感激。我对Javascript相当陌生,我已经挠头很久了


感谢获得您所能做的平均值

//---Wave Height---------------------------------- 
    var averageMaxBreakingHeight=0;
    var numberOfValues=0;
    $.each(data, function(){
        averageMaxBreakingHeight+=parseFloat(this.swell.maxBreakingHeight);
            numberOfValues++;
        /*$('.test').append('<p>' + this.swell.maxBreakingHeight + '</p>'); */
    });
    averageMaxBreakingHeight=averageMaxBreakingHeight/numberOfValues;
    $('.test').append('<p>'+averageMaxBreakingHeight+'</p>');


     //---Wind Direction---------------------------------- 
综上所述,

Math.round(parseFloat(numbers));
要将数字四舍五入到小数点后两位

Math.round(parseFloat(numbers) * 100) / 100;

请在您的问题中包含所有相关代码。不要只提供链接如果fiddle在那里提供链接…甚至连链接都没有。@rene:请修复您的编辑,您已经删除了链接@穆塞凡:是的,我想解决这个问题,但你们打败了我。