2016-05-10 8 views
1

"toursByHotels"の下にあるオブジェクトの配列を取る - このデータを取得して、同じ名前(ホテルの名前、つまりマリオットなど)を持つ各オブジェクトのツアーの合計を取得します。名前でオブジェクトを整理する

同じ名前の5つの新しい配列を作成する必要があるより簡単な方法はありますか?

var toursByHotel = [ 
 
     { 
 
      "name": "Marriott", 
 
      "month": 1, 
 
      "tours": 10 
 
     }, 
 
     { 
 
      "name": "Marriott", 
 
      "month": 2, 
 
      "tours": 15 
 
     }, 
 
     { 
 
      "name": "Marriott", 
 
      "month": 3, 
 
      "tours": 8 
 
     }, 
 
     { 
 
      "name": "Marriott", 
 
      "month": 4, 
 
      "tours": 12 
 
     }, 
 
     { 
 
      "name": "Marriott", 
 
      "month": 5, 
 
      "tours": 18 
 
     }, 
 
     { 
 
      "name": "Marriott", 
 
      "month": 6, 
 
      "tours": 25 
 
     }, 
 
     { 
 
      "name": "Marriott", 
 
      "month": 7, 
 
      "tours": 40 
 
     }, 
 
     { 
 
      "name": "Marriott", 
 
      "month": 8, 
 
      "tours": 33 
 
     }, 
 
     { 
 
      "name": "Marriott", 
 
      "month": 9, 
 
      "tours": 25 
 
     }, 
 
     { 
 
     "name": "Marriott", 
 
      "month": 10, 
 
      "tours": 21 
 
     }, 
 
     { 
 
     "name": "Marriott", 
 
      "month": 11, 
 
      "tours": 18 
 
     }, 
 
     { 
 
     "name": "Marriott", 
 
      "month": 12, 
 
      "tours": 14 
 
     }, 
 
     { 
 
      "name": "Springhill", 
 
      "month": 1, 
 
      "tours": 10 
 
     }, 
 
     { 
 
      "name": "Springhill", 
 
      "month": 2, 
 
      "tours": 15 
 
     }, 
 
     { 
 
      "name": "Springhill", 
 
      "month": 3, 
 
      "tours": 8 
 
     }, 
 
     { 
 
      "name": "Springhill", 
 
      "month": 4, 
 
      "tours": 12 
 
     }, 
 
     { 
 
      "name": "Springhill", 
 
      "month": 5, 
 
      "tours": 18 
 
     }, 
 
     { 
 
      "name": "Springhill", 
 
      "month": 6, 
 
      "tours": 25 
 
     }, 
 
     { 
 
      "name": "Springhill", 
 
      "month": 7, 
 
      "tours": 40 
 
     }, 
 
     { 
 
      "name": "Springhill", 
 
      "month": 8, 
 
      "tours": 33 
 
     }, 
 
     { 
 
      "name": "Springhill", 
 
      "month": 9, 
 
      "tours": 25 
 
     }, 
 
     { 
 
     "name": "Springhill", 
 
      "month": 10, 
 
      "tours": 21 
 
     }, 
 
     { 
 
     "name": "Springhill", 
 
      "month": 11, 
 
      "tours": 18 
 
     }, 
 
     { 
 
     "name": "Springhill", 
 
      "month": 12, 
 
      "tours": 14 
 
     }, 
 
     { 
 
      "name": "Residence", 
 
      "month": 1, 
 
      "tours": 10 
 
     }, 
 
     { 
 
      "name": "Residence", 
 
      "month": 2, 
 
      "tours": 15 
 
     }, 
 
     { 
 
      "name": "Residence", 
 
      "month": 3, 
 
      "tours": 8 
 
     }, 
 
     { 
 
      "name": "Residence", 
 
      "month": 4, 
 
      "tours": 12 
 
     }, 
 
     { 
 
      "name": "Residence", 
 
      "month": 5, 
 
      "tours": 18 
 
     }, 
 
     { 
 
      "name": "Residence", 
 
      "month": 6, 
 
      "tours": 25 
 
     }, 
 
     { 
 
      "name": "Residence", 
 
      "month": 7, 
 
      "tours": 40 
 
     }, 
 
     { 
 
      "name": "Residence", 
 
      "month": 8, 
 
      "tours": 33 
 
     }, 
 
     { 
 
      "name": "Residence", 
 
      "month": 9, 
 
      "tours": 25 
 
     }, 
 
     { 
 
     "name": "Residence", 
 
      "month": 10, 
 
      "tours": 21 
 
     }, 
 
     { 
 
     "name": "Residence", 
 
      "month": 11, 
 
      "tours": 18 
 
     }, 
 
     { 
 
     "name": "Residence", 
 
      "month": 12, 
 
      "tours": 14 
 
     }, 
 
     { 
 
      "name": "Courtyard", 
 
      "month": 1, 
 
      "tours": 10 
 
     }, 
 
     { 
 
      "name": "Courtyard", 
 
      "month": 2, 
 
      "tours": 15 
 
     }, 
 
     { 
 
      "name": "Courtyard", 
 
      "month": 3, 
 
      "tours": 8 
 
     }, 
 
     { 
 
      "name": "Courtyard", 
 
      "month": 4, 
 
      "tours": 12 
 
     }, 
 
     { 
 
      "name": "Courtyard", 
 
      "month": 5, 
 
      "tours": 18 
 
     }, 
 
     { 
 
      "name": "Courtyard", 
 
      "month": 6, 
 
      "tours": 25 
 
     }, 
 
     { 
 
      "name": "Courtyard", 
 
      "month": 7, 
 
      "tours": 40 
 
     }, 
 
     { 
 
      "name": "Courtyard", 
 
      "month": 8, 
 
      "tours": 33 
 
     }, 
 
     { 
 
      "name": "Courtyard", 
 
      "month": 9, 
 
      "tours": 25 
 
     }, 
 
     { 
 
     "name": "Courtyard", 
 
      "month": 10, 
 
      "tours": 21 
 
     }, 
 
     { 
 
     "name": "Courtyard", 
 
      "month": 11, 
 
      "tours": 18 
 
     }, 
 
     { 
 
     "name": "Courtyard", 
 
      "month": 12, 
 
      "tours": 14 
 
     }, 
 
     { 
 
      "name": "Renaissance", 
 
      "month": 1, 
 
      "tours": 10 
 
     }, 
 
     { 
 
      "name": "Renaissance", 
 
      "month": 2, 
 
      "tours": 15 
 
     }, 
 
     { 
 
      "name": "Renaissance", 
 
      "month": 3, 
 
      "tours": 8 
 
     }, 
 
     { 
 
      "name": "Renaissance", 
 
      "month": 4, 
 
      "tours": 12 
 
     }, 
 
     { 
 
      "name": "Renaissance", 
 
      "month": 5, 
 
      "tours": 18 
 
     }, 
 
     { 
 
      "name": "Renaissance", 
 
      "month": 6, 
 
      "tours": 25 
 
     }, 
 
     { 
 
      "name": "Renaissance", 
 
      "month": 7, 
 
      "tours": 40 
 
     }, 
 
     { 
 
      "name": "Renaissance", 
 
      "month": 8, 
 
      "tours": 33 
 
     }, 
 
     { 
 
      "name": "Renaissance", 
 
      "month": 9, 
 
      "tours": 25 
 
     }, 
 
     { 
 
     "name": "Renaissance", 
 
      "month": 10, 
 
      "tours": 21 
 
     }, 
 
     { 
 
     "name": "Renaissance", 
 
      "month": 11, 
 
      "tours": 18 
 
     }, 
 
     { 
 
     "name": "Renaissance", 
 
      "month": 12, 
 
      "tours": 14 
 
     } 
 
     ];

答えて

1

あなたはreduceを使用して、各キーは、ホテル名と値はツアーの和であるオブジェクトを返すことができます。 DEMO

data = data.reduce(function(obj, e) { 
    obj[e.name] = (obj[e.name] || 0) + e.tours; 
    return obj; 
}, {}); 

console.log(data) 

あなたは各ホテルのための和との割合を取得したい場合は、これを実行するための最良の方法だろう削減使用して、このDEMO

var result = {} 

var total = data.reduce((a, b) => {return a + b.tours }, 0); 
data.forEach(function(e) { 
    if(!this[e.name]) { 
    this[e.name] = {sum: e.tours, percentage: 0} 
    result[e.name] = this[e.name]; 
    } 
    this[e.name].sum += e.tours; 
    this[e.name].percentage = (this[e.name].sum/total)*100; 
}, {}); 

console.log(result) 
+0

それがこのコードのエラーの多くを取得... "e" が定義されていないと言います。 – Krang

+0

@それはフィドルでうまく動作します。 –

+0

- うん、それは実際にはフィドルで動作します。何らかの理由で私のコードで試してみると "結果が定義されていません"というメッセージが表示されます – Krang

1

のようにそれを行うことができます。

Working Example

// lets keep track of the total here: 
var total = 0; 

var totals = a.reduce(function(curr, next) { 
    curr[next.name] = (curr[next.name] || 0) + next.tours; 
    // increment total here: 
    total += next.tours; 
    return curr; 
}, {}); 

for (var hotel in totals) { 
    var obj = {}; 
    obj.name = totals[hotel]; 
    obj.percentage = (totals[hotel]/total) * 100 + '%'; 
    totals[hotel] = obj; 
} 

MDN docs on .reduce

+0

@JoranHendrix - ありがとうございました。変数(合計)に保存されたすべての数字の合計。各ホテルのツアーの合計の何パーセントを構成するのか – Krang

+0

あなたは答えを選択したことを知っていますが、私は本当に複雑さが気に入らないので、もっと簡単なアプローチで答えを更新しています... – JordanHendrix

関連する問題