猿问

对对象数组的每 5 个值求和

假设我有一个像这样不确定长度的对象数组


jString = [

  {

    "value": "2,335.47"

  },

  {

    "value": "2,862.96"

  },

  {

    "value": "2,217.31"

  },

  {

    "value": "1,627.01"

  },

  {

    "value": "1,103.52"

  },

  {

    "value": "1,718.69"

  },

  {

    "value": "1,992.83"

  },

  {

    "value": "3,796.26"

  },

  {

    "value": "3,800.43"

  },

  {

    "value": "2,128.62"

  },

  {

    "value": "2,661.56"

  }

]

我需要获得每 5 个元素的总和平均值并作为新数组返回


我认为地图功能效果最好,但它对我不起作用。


 function sumAvg(arr) {

            var sum = 0;

            avg= arr.map(function(item){

              for(var i = 0; i <arr.length -1; i++){

                sum = (item.value[i] + item.value[i+1] + item.value[i+2] + item.value[i+3] + item.value[i+4])/5;

              }

              return item = sum;

            })

            return avg;

           }


           var avgString= sumAvg(jString);


           console.log(avgString);


一只名叫tom的猫
浏览 199回答 3
3回答

阿晨1998

您的问题有点不清楚您是想要每个连续 5 个元素的总和还是平均值。这是获取总和的代码,还有一些关于如何从中获取平均值的附加说明。const data = [{"value":"2,335.47"},{"value":"2,862.96"},{"value":"2,217.31"},{"value":"1,627.01"},{"value":"1,103.52"},{"value":"1,718.69"},{"value":"1,992.83"},{"value":"3,796.26"},{"value":"3,800.43"},{"value":"2,128.62"},{"value":"2,661.56"}];// Create a new arrayconst out = [];// While there are elements remainingwhile (data.length > 0) {&nbsp; // `splice` off 5 elements from the array&nbsp; const next = data.splice(0, 5)&nbsp; // `reduce` over those 5 elements adding up the values&nbsp; // Note 1: If you want to get the average just add `/ 5`&nbsp; // to the end of this expression&nbsp; // Note 2: we have to remove the comma from the value string in&nbsp; // order to parse it properly&nbsp; const sum = next.reduce((a, b) => {&nbsp; &nbsp; return a + Number.parseFloat(b.value.replace(/,/g, ''));&nbsp; }, 0);&nbsp; // Push the stringified sum to the output array.&nbsp; out.push(sum.toFixed(2));}console.log(out);进一步阅读splicereducetoFixed

互换的青春

地图不会以您想要的方式工作。您正在对每个 5 个元素组进行计算,其中一些计算 4 次。您可以获取前五个元素,进行平均,将其存储在某处,然后将其删除。重复这个直到数组为空

扬帆大鱼

function sumAvg(arr) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var sum = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; avg= arr.map(function(item){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(var i = 0; i <arr.length -1; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum = (item.value[i] + item.value[i+1] + item.value[i+2] + item.value[i+3] + item.value[i+4])/5;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return item = sum;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return avg;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var avgString= sumAvg(jString);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log(avgString);将 for 循环中的 sum 更改为如下所示:for(var i = 0; i <arr.length -1; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum += item.value[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }然后在 for 循环内你可以有如下内容:if i == 5 thensum = sum/5;end if这将返回 json 中 5 个项目总和的平均值。我假设这就是你要找的,我不推荐这个 sum = (item.value[i] + item.value[i+1] + item.value[i+2] + item.value[i+ 3] + item.value[i+4])/5; 因为您会遇到索引错误,尤其是在 json 的长度未知的情况下。如果您正在查看求和的平均值,则在将 5 个值加在一起后,将 if 语句调整为:if i % 5 == 0 thensum = sum/5;end if希望这有任何帮助。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答