2016-08-30 14 views
2

私自身のアプリケーションを構築しようとしていますが、私は解決できない問題を抱えています。 私のアプリは住宅ローンを扱っています。私はいくつかのモーゲージを並行して運営しており、期間ごとの合計支払い額を合計するだけでよいのです。Javascript配列:値ごとの一意の値と集計値によるグループ

のは、我々は次のオブジェクトの配列(すなわち住宅ローン)を持っているとしましょう:

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

この(第一ラインの一例)を読み取る方法:「月数84まで、私は月額$ 150支払います」 。

私はそのような結果配列を得るために、「uptomonth」でオブジェクトを並べ替える、1列(例えばarray.concatを使用して...)への配列を結合したい:

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

最も困難私にとっては、(この値の重複があるので) "アップトムーンズ"でグループ化し、 "アップトーンオン"あたりの合計支払いを得ることです...

どのようにするか考えていますか? ありがとう!

+2

[SOI]はコード作成サービスではありません:これまでに試したことを示してください。 (ヒント:それ自体でグループを解決してください) – Richard

+0

したがって、たとえばumonth:120の支払い額は300(100 + 200)にする必要がありますか? –

答えて

0

あなたはこのように、この作業を行うことができます。

  1. オブジェクトの配列への配列新しい配列
  2. がuptomonthによって
  3. グループの総支払を計算し、それぞれの支払いを求めることのみ
  4. ソートを平らに1

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)

0

変数ans、ご希望の配列であり、この

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)}); 

を試してみてください。

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() 
関連する問題