【问题标题】:Cumulative sum of values belonging to a same field in an array数组中属于同一字段的值的累积和
【发布时间】:2020-04-19 00:24:21
【问题描述】:

我有一个以下格式的数组,其中包含 x 和 y 值以及一个数据字段,我希望将其转换为一个数组,该数组将特定数据上的所有单个值相加,然后显示一个累积值(求和超过该日期,然后将该值添加到以前的日期)。例如。

data_array = [
{xvalue: 10, yvalue: 5, date: "12/2"},
{xvalue: 10, yvalue: 5, date: "12/2"},
{xvalue: 8, yvalue: 3, date: "12/3"},
{xvalue: 10, yvalue: 5, date: "12/3"},
{xvalue: 6, yvalue: 1, date: "12/4"},
{xvalue: 20, yvalue: 3, date: "12/4"},
]

新数组

new_data = [
{xvalue: 20, yvalue: 10, date: "12/2"}
{xvalue: 38, yvalue: 18, date: "12/3"}
{xvalue: 64, yvalue: 22, date: "12/4"}
]

有什么方法可以有效地做到这一点?目前我有以下可行的方式,但感觉它不是最好的实现

var temp = {}
var obj = null
for (var i=0; i<data_array.length; i++){
    obj = data_array[i];

    if (!temp[obj.date]) {
        temp[obj.date]=obj;
    } 
    else {
        temp[obj.date].xvalue += obj.xvalue;
        temp[obj.date].yvalue += obj.yvalue
    }
}
var result = [];
for (var prop in temp){
    result.push(temp[prop]);
}
console.log("result", result)

for (var i=0; i< result.length; i++){
  if (i!=0){
    result[i].xvalue +=result[i-1].xvalue;
    result[i].yvalue +=result[i-1].yvalue;
  }
}

【问题讨论】:

标签: javascript arrays sorting multidimensional-array javascript-objects


【解决方案1】:

data_array = [
{xvalue: 10, yvalue: 5, date: "12/2"},
{xvalue: 10, yvalue: 5, date: "12/4"},
{xvalue: 8, yvalue: 3, date: "12/3"},
{xvalue: 10, yvalue: 5, date: "12/2"},
{xvalue: 6, yvalue: 1, date: "12/3"},
{xvalue: 20, yvalue: 3, date: "12/2"},
];
var reduced =  data_array.reduce(function(final,item){
  if(final[item.date]){
    final[item.date] = {cummxvalue: item.xvalue+final[item.date].cummxvalue, cummyvalue: item.yvalue+final[item.date].cummyvalue, date: item.date};
  }else{
    final[item.date] = {cummxvalue: item.xvalue, cummyvalue: item.yvalue, date: item.date};
  }
return final;
},{});
var new_data = Object.values(reduced);
console.log(new_data); 

【讨论】:

  • 我可以建议使用Object.values(reduced),而不是最后的for循环吗?
  • 谢谢@Geetanjali!我用我的实现更新了我的问题,因为我还需要在该日期内进行累积总和。对这种实施有什么想法?
【解决方案2】:

您可以遍历数组并添加xvalues 和yvalue,如果元素不具有相同的date 值,则可以随时复制到新数组中。您还需要对数组进行排序,以防它尚未作为您的第一个示例进行排序。

const data_array = [
  {xvalue: 10, yvalue: 5, date: "12/2"},
  {xvalue: 10, yvalue: 5, date: "12/4"},
  {xvalue: 8, yvalue: 3, date: "12/3"},
  {xvalue: 10, yvalue: 5, date: "12/2"},
  {xvalue: 6, yvalue: 1, date: "12/3"},
  {xvalue: 20, yvalue: 3, date: "12/2"}
]

let new_data = [];
let cummxvalue = 0;
let cummyvalue = 0;

data_array.sort( (a, b) => a.date > b.date );

for( const obj of data_array) {
  let o = new_data.find(o => o.date === obj.date);
  cummxvalue += obj.xvalue;
  cummyvalue += obj.yvalue;

  if(!o) {
    new_data.push({cummxvalue: cummxvalue, cummyvalue: cummyvalue, date: obj.date});
  }
  else {
    o.cummxvalue = cummxvalue;
    o.cummyvalue = cummyvalue;
  }
}

console.log(new_data)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    • 2014-01-03
    • 1970-01-01
    • 2018-12-04
    • 2021-11-27
    • 2013-07-13
    相关资源
    最近更新 更多