2016-05-10 12 views
1

Nehmen Sie die Anordnung der Objekte unter "toursByHotels" - Ich möchte diese Daten nehmen und die Summe der Touren für jedes Objekt mit dem gleichen Namen (der Name des Hotels, dh. Marriott, etc).Organisieren von Objekten nach Namen

Gibt es einen einfacheren Weg in d3, als 5 neue Arrays mit denselben Namen zu erstellen, und die Touren dann summieren?

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

Antwort

1

können Sie reduce verwenden und Objekt zurück, wo jeder Schlüssel ist, Hotelname und Wertsumme der Touren ist. DEMO

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

console.log(data) 

Wenn Sie Summe und den Prozentsatz für jedes Hotel bekommen möchten, können Sie es tun, wie diese 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

es sagt, "e" nicht definiert ist ... viele Fehler mit diesem Code zu bekommen. – Krang

+0

@Krang es funktioniert gut in der Geige. –

+0

- Ja, wahr, es funktioniert in der Geige. aus irgendeinem Grund, wenn ich es in meinem Code versuche ich bekomme "Ergebnis ist nicht definiert" – Krang

1

reduzieren Verwendung wäre der beste Weg, dies zu tun:

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 - danke - von diesem wenn ich hätte eine Gesamtsumme all dieser Zahlen, die in einer Variablen gespeichert wurden (total), wie würde ich herausfinden, wie viel Prozent der gesamten Tour jedes Hotels ausmachen? – Krang

+0

hey Ich weiß, dass du eine Antwort ausgewählt hast, aber ich mag die Komplexität dort wirklich nicht, ich aktualisiere meine Antwort mit einem viel einfacheren Ansatz ... – JordanHendrix

Verwandte Themen