Javascript数组:按唯一值分组和每个值的总结金额

时间:2016-08-30 08:51:19

标签: javascript arrays grouping

我正在尝试构建自己的应用程序,而且我遇到了一个我无法解决的问题。 我的应用程序处理抵押贷款。我有几个并行的抵押贷款,我只需要总结每期的总付款。

假设我们有以下对象数组(即抵押):

[ {uptomonth:84 , payment:150} ] 
[ {uptomonth:120 , payment:200} ] 
[ {uptomonth:120 , payment:100} , {uptomonth:180 , payment:250} , {uptomonth:300 , payment:500} ] 

如何阅读本文(第一行的例子):“最多一个月号84,我每月支付150美元”。

我想将数组合并到一个数组中(例如使用array.concat ...),然后按“uptomonth”对对象进行排序,以获得类似的结果数组:

[ {uptomonth:84,payment:1200} , {uptomonth:120,payment:1050} , {uptomonth:180,payment:750} , {uptomonth:300,payment:500} ] 

对我来说最困难的是按“uptomonth”分组(因为这个值有重复),并获得每个“uptomonth”的总付款......

知道如何做到这一点? 非常感谢!

3 个答案:

答案 0 :(得分:0)

你可以这样做:

  1. 将数组展平为仅包含对象的数组
  2. 对新数组进行排序
  3. 计算总付款
  4. 按照uptomonth分组并计算每个人的付款
  5. var data = [[ {uptomonth:84 , payment:150} ],[ {uptomonth:120 , payment:200} ],[ {uptomonth:120 , payment:100} , {uptomonth:180 , payment:250} , {uptomonth:300 , payment:500} ] ];
    
    var finalResult = [];
    
    //Flatten the array
    var newArray = data.reduce(function(r, a) {
      a.forEach(function(o) {r.push(o)});
      return r;
    }, [])
    
    //Sort the array
    newArray.sort(function(a, b) {
      return a.uptomonth - b.uptomonth;
    });
    
    //Get total payment
    var total = newArray.reduce(function(r, a) {
      return r = r + a.payment;
    }, 0)
    
    //Group by uptomonth and calculate payment for each one
    newArray.forEach(function(o) {
      if (!this.payment) this.payment = total;
      if (!this[o.uptomonth]) {
        this[o.uptomonth] = {uptomonth: o.uptomonth, payment: this.payment}
        finalResult.push(this[o.uptomonth]);
      }
      this.payment -= o.payment;
    }, {});
    
    console.log(finalResult)

答案 1 :(得分:0)

试试这个

var array1 = [2, 5, 8]; // 2 is solved, 5 is solved, 8 is unsolved
var array2 = [2, 5, 6]; // every element is solved

function isSolvedString(operand) {
  return operand < 8 ? 'solved' : 'unsolved';
}

function isSolved(current) {
  return isSolvedString(current) === 'solved' ? true : false;
}

console.log(array1.every(isSolved)); // false
console.log(array2.every(isSolved)); // true

变量var data = [[ {uptomonth:84 , payment:150} ],[ {uptomonth:120 , payment:200} ],[ {uptomonth:120 , payment:100} , {uptomonth:180 , payment:250} , {uptomonth:300 , payment:500} ] ]; var newData = {}; data.forEach(function(array) { array.forEach(function(node) { if(!newData[node.uptomonth]){newData[node.uptomonth]=0}; newData[node.uptomonth] += node.payment; }); }); var ans = []; Object.keys(newData).forEach(function(key){ var abc = {}; abc.uptomonth = key; abc.payment=newData[key]; ans.push(abc); }); ans.sort(function(a,b){return parseInt(a.uptomonth)>parseInt(b.uptomonth)}); 是您想要的数组。

答案 2 :(得分:0)

我建议您使用lodash中的_.flatten方法。

var data = [[ {uptomonth:84 , payment:150} ], 
            [ {uptomonth:120 , payment:200} ] ,
            [ {uptomonth:120 , payment:100} , 
              {uptomonth:180 , payment:250} , 
              {uptomonth:300 , payment:500} ]]

_
 .chain(data)
 .flatten()
 .reduce((acc, obj) => {
   var month = obj.uptomonth
   var payment = obj.payment

   if (acc[month]) acc[month] += payment
   else acc[month] = payment

   return acc
 }, {})
 .value()