Saya mencoba menjumlahkan semua nilai "Menge" dan "Fehler" jika "Datum" sama. Masalahnya adalah bahwa "Datum" adalah objek tanggal.

var arr = [{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":100,"Fehler":5},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":5,"Fehler":1},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":6,"Fehler":65},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"222","Menge":10,"Fehler":5},{"Datum":{"date":"2000-01-02 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"444","Menge":29,"Fehler":1},{"Datum":{"date":"2000-01-02 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":1,"Fehler":1}]

console.log(arr)
var holder = {};

arr.forEach(function (d) {
    if (holder.hasOwnProperty(d.Datum)) {
        holder[d.Datum] = holder[d.Datum] + d.Menge;
    } else {
        holder[d.Datum] = d.Menge;
    }
});

var obj2 = [];

for (var prop in holder) {
    obj2.push({
        Datum: prop,
        Menge: holder[prop]
    });
}

console.log(obj2);

Tapi hasilnya harus:

[
  {
    "Datum": "2000-01-01",
    "Menge": 121,
    "Fehler": 76
  },
  {
    "Datum": "2000-01-02",
    "Menge": 30,
    "Fehler": 2
  }
]
3
InFlames82 13 Januari 2022, 13:48

5 jawaban

Jawaban Terbaik

Jenis cara ES6 dan pengelompokan berdasarkan solusi hash:

const arr = [{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":100,"Fehler":5},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":5,"Fehler":1},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":6,"Fehler":65},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"222","Menge":10,"Fehler":5},{"Datum":{"date":"2000-01-02 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"444","Menge":29,"Fehler":1},{"Datum":{"date":"2000-01-02 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":1,"Fehler":1}]

const result = Object.values(arr.reduce((acc, obj) => {
  const [Datum] = obj.Datum.date.split(' ');
  const Menge = (acc[Datum]?.Menge + obj.Menge) || obj.Menge;
  const Fehler = (acc[Datum]?.Fehler + obj.Fehler) || obj.Fehler;
  const Ratio = Fehler / Menge;
  acc[Datum] = { Datum, Menge, Fehler, Ratio };
  return acc;
}, {}));

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}
1
Alexandr Belan 13 Januari 2022, 15:58
Terima kasih. pertanyaan tambahan: Bagaimana cara membuat "Menge" dan "Fehler" kunci baru "rasio" tambahan di mana saya menghitung rasio Fehler/Menge? Saya tidak tahu cara mengakses nilai Menge dan Fehler dalam kode Anda untuk menghitung rasio.
 – 
InFlames82
13 Januari 2022, 15:43
Saya senang bisa membantu! Periksa kode yang diperbarui dengan properti rasio
 – 
Alexandr Belan
13 Januari 2022, 15:59

Jika Anda mengabaikan zona waktu dan bagian waktu, Anda dapat mengekstrak bagian tanggal seperti di bawah ini dan menggunakannya sebagai kunci untuk holder.

var arr = [{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":100,"Fehler":5},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":5,"Fehler":1},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":6,"Fehler":65},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"222","Menge":10,"Fehler":5},{"Datum":{"date":"2000-01-02 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"444","Menge":29,"Fehler":1},{"Datum":{"date":"2000-01-02 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":1,"Fehler":1}]

var holder = {};

arr.forEach(function (d) {
const dateStr = d.Datum.date.substring(0, 10);

  if (holder.hasOwnProperty(dateStr)) {
    var existing = holder[dateStr];
    holder[dateStr] = {
        Datum: dateStr,
        Menge: existing.Menge + d.Menge,
        Fehler: existing.Fehler + d.Fehler,
    };
  } else {
    holder[dateStr] = {
        Datum: dateStr,
        Menge: d.Menge,
        Fehler: d.Fehler,
    };
  }
});

var obj2 = Object.values(holder);
console.log(obj2);
1
Udith Gunaratna 13 Januari 2022, 14:12
Terima kasih! Tapi bagaimana dengan jumlah "Fehler"? Anda dapat melihatnya di contoh hasil saya.
 – 
InFlames82
13 Januari 2022, 14:11
Memperbarui kode untuk memasukkan jumlah "Fehler" dan juga menghapus kebutuhan untuk loop ke-2
 – 
Udith Gunaratna
13 Januari 2022, 14:13

Cara alternatif;

var arr = [{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":100,"Fehler":5},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":5,"Fehler":1},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":6,"Fehler":65},{"Datum":{"date":"2000-01-01 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"222","Menge":10,"Fehler":5},{"Datum":{"date":"2000-01-02 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"444","Menge":29,"Fehler":1},{"Datum":{"date":"2000-01-02 00:00:00.000000","timezone_type":3,"timezone":"Europe/Berlin"},"Material":"123","Menge":1,"Fehler":1}]

var Arrr = [];

arr.forEach(function (d) {
var dateArr = d.Datum.date.split(" ");
if(Arrr.find(x=> x.Datum ===dateArr[0]))
{
    Arrr.find(x=> x.Datum ===dateArr[0]).Fehler += d.Fehler;
    Arrr.find(x=> x.Datum ===dateArr[0]).Menge += d.Menge;
}
else {
    Arrr.push({
        Datum: dateArr[0],
        Menge: d.Menge,
        Fehler:d.Fehler
    });
}
});

console.log(Arrr);
1
cacalonga 13 Januari 2022, 15:04

Alternatif, jika zona waktu Anda memiliki pengaruh, Anda dapat merangkai Datum Anda, seperti ini:

var arr = [{ "Datum": { "date": "2000-01-01 00:00:00.000000", "timezone_type": 3, "timezone": "Europe/Berlin" }, "Material": "123", "Menge": 100, "Fehler": 5 }, { "Datum": { "date": "2000-01-01 00:00:00.000000", "timezone_type": 3, "timezone": "Europe/Berlin" }, "Material": "123", "Menge": 5, "Fehler": 1 }, { "Datum": { "date": "2000-01-01 00:00:00.000000", "timezone_type": 3, "timezone": "Europe/Berlin" }, "Material": "123", "Menge": 6, "Fehler": 65 }, { "Datum": { "date": "2000-01-01 00:00:00.000000", "timezone_type": 3, "timezone": "Europe/Berlin" }, "Material": "222", "Menge": 10, "Fehler": 5 }, { "Datum": { "date": "2000-01-02 00:00:00.000000", "timezone_type": 3, "timezone": "Europe/Berlin" }, "Material": "444", "Menge": 29, "Fehler": 1 }, { "Datum": { "date": "2000-01-02 00:00:00.000000", "timezone_type": 3, "timezone": "Europe/Berlin" }, "Material": "123", "Menge": 1, "Fehler": 1 }]

var holder = new Map();

arr.forEach(function (d) {
  const strDatum = JSON.stringify(d.Datum);
  const current = holder.get(strDatum) || { Menge: 0, Fehler: 0};

  holder.set(strDatum, { Menge: current.Menge + d.Menge, Fehler: current.Fehler + d.Fehler });
});

var obj2 = [];

holder.forEach((value, key) => {
  obj2.push({
    Datum: JSON.parse(key),
    Menge: value.Menge,
    Fehler: value.Fehler,
  });
});

console.log(obj2);
0
PZBird 13 Januari 2022, 14:18

Tanpa Menggunakan JS split() atau substring()

Gunakan Date() untuk menentukan hasil. Saya hanya punya saran di atas jawaban Udith. Anda dapat melakukan beberapa hal seperti ini:

// This can easily give you the results without even worrying for any substring stuff
const date = new Date(obj.Datum.date)

Kode Akhir:

var dataArray = [{ "Datum": { "date": "2000-01-01 00:00:00.000000", "timezone_type": 3, "timezone": "Europe/Berlin" }, "Material": "123", "Menge": 100, "Fehler": 5 }, { "Datum": { "date": "2000-01-01 00:00:00.000000", "timezone_type": 3, "timezone": "Europe/Berlin" }, "Material": "123", "Menge": 5, "Fehler": 1 }, { "Datum": { "date": "2000-01-01 00:00:00.000000", "timezone_type": 3, "timezone": "Europe/Berlin" }, "Material": "123", "Menge": 6, "Fehler": 65 }, { "Datum": { "date": "2000-01-01 00:00:00.000000", "timezone_type": 3, "timezone": "Europe/Berlin" }, "Material": "222", "Menge": 10, "Fehler": 5 }, { "Datum": { "date": "2000-01-02 00:00:00.000000", "timezone_type": 3, "timezone": "Europe/Berlin" }, "Material": "444", "Menge": 29, "Fehler": 1 }, { "Datum": { "date": "2000-01-02 00:00:00.000000", "timezone_type": 3, "timezone": "Europe/Berlin" }, "Material": "123", "Menge": 1, "Fehler": 1 }];

const holder = {};

dataArray.forEach(obj => {
  // Using Date() of Javascript for converting the timestamp to a 
  // a date object for better computation
  const objDate = new Date(obj.Datum.date);
  if(holder.hasOwnProperty(objDate)){
    let existingData = holder[objDate];
    holder[objDate] = {
        Datum: obj.Datum.date,
        Menge: existingData.Menge + obj.Menge,
        Fehler: existingData.Fehler + obj.Fehler
    };
  }else{
    holder[objDate] = {
      Datum: obj.Datum.date,
      Menge: obj.Menge,
      Fehler: obj.Fehler
    }
  }
});

const resultObj = Object.values(holder);
console.log(resultObj);
0
Alok 13 Januari 2022, 15:01