Ich habe eine Reihe von Daten im folgenden Format

let people=[
  {
    aggregationType: "1",
    coefficient: 0.03,
    id: "p1",
    name: "petter",
    type: "Number",
    age: 14
  },
  {
    aggregationType: "0",
    coefficient: 1,
    id: "p2",
    name: "mary",
    type: "Number",
    value: 24
  },
  {
    aggregationType: "1",
    coefficient: 0.03,
    id: "p1",
    name: "Amee",
    type: "Number",
    value: 32
  },
  {
    aggregationType: "0",
    coefficient: 1,
    id: "p2",
    name: "Mtp",
    type: "Number",
    value: 33
  },
  {
    aggregationType: "1",
    coefficient: 0.03,
    id: "p1",
    name: "Louis",
    type: "Number",
    value: 44
  },
]

Ich möchte den Datensatz nach der 'ID' gruppieren und die Bedingung überprüfen: Wenn der Schlüsselaggregationstyp = 0 ist, berechnen Sie das Summenalter, wenn der Schlüsselaggregationstyp = 1, berechnen Sie das Durchschnittsalter. Und multiplizieren Sie mit dem Schlüssel 'Koeffizient', so dass das gewünschte Ergebnis so sein sollte.

output=[
  [
    "p1", 0.9  // equivalent to ((14 + 32 + 44)/3)*0.03 aggregationType = 1 ==> calculate avg age,coefficient: 0.03
  ],
  [
    "p2", 57  // equivalent to (24+33)*1 = 57  aggregationType = 0 ==> calculate sum age, coefficient: 1
  ]
]

Ich habe versucht, diese Funktion zu schreiben, weiß aber nicht, wie ich fortfahren soll.

let result = {};

for ( let { id, aggregationType, value } of task ) 
{ 
  result[id] = result[id] || [];
  result[id].push({aggregationType, value }); 
}
console.log(result);
/*
// group by id
result = {
  p1: [
    {
      aggregationType: "1",
      value: 14
    },
    {
      aggregationType: "1",
      value: 32
    },
    {
      aggregationType: "1",
      value: 44
    }
  ],
  p2: [
    {
      aggregationType: "0",
      value: 24
    },
    {
      aggregationType: "0",
      value: 33
    },
    
  ]
}

*/
let newResult=[];
for (let [key, value] of Object.entries(result)) {
    newResult = [key, value.reduce((a, e) => {
    if (e.aggregationType === '0') {
        a += e.value ;
    } else {
       (a += e.value) / value.length;
    }
     return a;
}, 0)]
}

Bitte helfen Sie mir, danke!

0
oneeyes 18 Aug. 2020 im 20:44

3 Antworten

Beste Antwort

Ciao, Sie könnten versuchen, die Funktion filter zu verwenden, um Ihre people basierend auf aggregation zu filtern, und dann ein forEach verwenden, um das Ergebnis zu berechnen:

let people=[
          {
            aggregationType: "1",
            coefficient: 0.03,
            id: "p1",
            name: "petter",
            type: "Number",
            value: 14
          },
          {
            aggregationType: "0",
            coefficient: 1,
            id: "p2",
            name: "mary",
            type: "Number",
            value: 24
          },
          {
            aggregationType: "1",
            coefficient: 0.03,
            id: "p1",
            name: "Amee",
            type: "Number",
            value: 32
          },
          {
            aggregationType: "0",
            coefficient: 1,
            id: "p2",
            name: "Mtp",
            type: "Number",
            value: 33
          },
          {
            aggregationType: "1",
            coefficient: 0.03,
            id: "p1",
            name: "Louis",
            type: "Number",
            value: 44
          },
        ]
        let result = [];
        let aggregationType = ["0", "1"];  //put here all the aggregation types you need
        aggregationType.forEach(aggregation => {
           let peopleFiltered = people.filter(el => el.aggregationType === aggregation)
           let sum = 0;
           let id = "";
           peopleFiltered.forEach(el => {
              sum = sum + (el.value * el.coefficient);
              id = el.id;
           })
           let partialResult = [];
           if (aggregation === "0") {
              partialResult = [ id, sum ]
           }
           else if(aggregation === "1") {
              partialResult = [ id, sum/peopleFiltered.length ]
           }
// extend this 'if...else if' to add new calculation for different aggregation
           
           result.push(partialResult)
        })

        
        console.log(result)
0
Giovanni Esposito 18 Aug. 2020 im 19:19
let people=[{aggregationType: "1",coefficient: 0.03,id: "p1",name: "petter",type: "Number",value: 14},{aggregationType: "0",coefficient: 1,id: "p2",name: "mary",type: "Number",value: 24},{aggregationType: "1",coefficient: 0.03,id: "p1",name: "Amee",type: "Number",value: 32},{aggregationType: "0",coefficient: 1,id: "p2",name: "Mtp",type: "Number",value: 33},{aggregationType: "1",coefficient: 0.03,id: "p1",name: "Louis",type: "Number",value: 44}];

var data = {};
people.forEach(function(item, index){
    data[item.id] = data[item.id] || {};
    data[item.id]['result'] = data[item.id]['result'] || 0;
    data[item.id]['values'] = data[item.id]['values'] || [];
    data[item.id]['values'].push(item.value)
    if('1' === item.aggregationType) {
        data[item.id]['result'] =  data[item.id]['values'].reduce(function(a,b){ return a + b},0) / data[item.id]['values'].length * item.coefficient;
    } else {
         data[item.id]['result'] = data[item.id]['result'] + item.value
    }

});

console.log(data.p1.result)
console.log(data.p2.result)
0
caramba 18 Aug. 2020 im 18:25

Da Sie mehr als zwei unterschiedliche id Werte haben könnten, würde ich vorschlagen, eine mit id verschlüsselte Karte zu erstellen, in der Sie in den entsprechenden Werten den entsprechenden Aggregationstyp speichern, den Koeffizienten und die Summe und Anzahl verfolgen.

Nach der Akkumulation können Sie die Werte aus dieser Map in das gewünschte Ausgabeformat extrahieren:

function aggregate(people) {
    let map = new Map;
    for (let {aggregationType, coefficient, id, age} of people) {
        let entry = map.get(id);
        if (!entry) map.set(id, entry = { aggregationType, coefficient, sum: 0, count: 0 });
        entry.sum += age;
        entry.count++;
    }
    return Array.from(map, ([id, { aggregationType, coefficient, sum, count}]) => 
        [id, (+aggregationType ? sum / count : sum) * coefficient]
    );
}

let people=[{aggregationType: "1",coefficient: 0.03,id: "p1",name: "petter",type: "Number",age: 14},{aggregationType: "0",coefficient: 1,id: "p2",name: "mary",type: "Number",age: 24},{aggregationType: "1",coefficient: 0.03,id: "p1",name: "Amee",type: "Number",age: 32},{aggregationType: "0",coefficient: 1,id: "p2",name: "Mtp",type: "Number",age: 33},{aggregationType: "1",coefficient: 0.03,id: "p1",name: "Louis",type: "Number",age: 44},]

console.log(aggregate(people));
1
trincot 18 Aug. 2020 im 18:11