Search code examples
javascriptarraysobjectsum

sum array and return object javascript


i have an array of objects as follows i want to return a data object which contains a data object and a total returns an array of objects from data and displays the data sum "jumlah"

const data = [
      {
        _id: "618ce59f6d45171be8f7986c",
        persediaanName: "beras_putih",
        funcL: "pembelian",
        namaP: "jeje",
        kuantitas: 3500,
        harga: 20000,
        tanggal: "2021-11-12T09:42:51.000Z",
        tahun: "2021",
        jumlah: 60000000,
        __v: 0,
      },
      {
        _id: "618ce6f36d45171be8f7989a",
        persediaanName: "beras_merah",
        funcL: "penjualan",
        namaP: "bibi",
        kuantitas: 200,
        harga: 20000,
        tanggal: "2021-11-12T09:48:31.000Z",
        tahun: "2021",
        jumlah: 4000000,
        __v: 0,
      },
      {
        _id: "618ce70e6d45171be8f798a2",
        persediaanName: "beras_putih",
        funcL: "penjualan",
        namaP: "gege",
        kuantitas: 200,
        harga: 100000,
        tanggal: "2021-11-12T09:49:00.000Z",
        tahun: "2021",
        jumlah: 2000000,
        __v: 0,
      },
      {
        _id: "618e0bf4804ae02c5c24d83e",
        persediaanName: "beras_merah",
        funcL: "piutang",
        namaP: "ayu",
        kuantitas: 5,
        harga: 1000,
        tanggal: "2021-11-13T06:38:40.000Z",
        tahun: "2021",
        jumlah: 5000,
        __v: 0,
      },
     
      
      {
        _id: "61b1fa6cfe099b0624f04cac",
        kategori: "beras",
        persediaanName: "beras_putih",
        funcL: "piutang",
        namaP: "cahya",
        kuantitas: 100,
        satuan: "kg",
        harga: 1221,
        tanggal: "2021-12-10T12:45:30.000Z",
        tahun: "2021",
        jumlah: 122100,
        __v: 0,
      },
    ];

return object data and total like this. jumlah: sum penjualan from beras_merah and piutang beras_merah. total : sum from data jumlah beras_merah and beras_putih.

const returnArr =  {
  data: [{
    persediaanName: "beras_merah",
    jumlah: // sum  penjualan beras_merah + piutang beras_merah
  },{
    persediaanName: "beras_putih",
    jumlah: // sum penjualan beras_putih + piutang beras_putih
  }]
  total: // sum jumlah "beras_merah" + "beras_puith"
}

Solution

  • This is one crude way of doing things: You can define the output model and assign initial values (in this case 0's) and iterate through your array and make necessary manipulations.

    If you have many values for persediaanName, then you can consider maintaining an array of those values and then do the crude way.

    const data = [{
        _id: "618ce59f6d45171be8f7986c",
        persediaanName: "beras_putih",
        funcL: "pembelian",
        namaP: "jeje",
        kuantitas: 3500,
        harga: 20000,
        tanggal: "2021-11-12T09:42:51.000Z",
        tahun: "2021",
        jumlah: 60000000,
        __v: 0,
      },
      {
        _id: "618ce6f36d45171be8f7989a",
        persediaanName: "beras_merah",
        funcL: "penjualan",
        namaP: "bibi",
        kuantitas: 200,
        harga: 20000,
        tanggal: "2021-11-12T09:48:31.000Z",
        tahun: "2021",
        jumlah: 4000000,
        __v: 0,
      },
      {
        _id: "618ce70e6d45171be8f798a2",
        persediaanName: "beras_putih",
        funcL: "penjualan",
        namaP: "gege",
        kuantitas: 200,
        harga: 100000,
        tanggal: "2021-11-12T09:49:00.000Z",
        tahun: "2021",
        jumlah: 2000000,
        __v: 0,
      },
      {
        _id: "618e0bf4804ae02c5c24d83e",
        persediaanName: "beras_merah",
        funcL: "piutang",
        namaP: "ayu",
        kuantitas: 5,
        harga: 1000,
        tanggal: "2021-11-13T06:38:40.000Z",
        tahun: "2021",
        jumlah: 5000,
        __v: 0,
      },
      {
        _id: "61b1fa6cfe099b0624f04cac",
        kategori: "beras",
        persediaanName: "beras_putih",
        funcL: "piutang",
        namaP: "cahya",
        kuantitas: 100,
        satuan: "kg",
        harga: 1221,
        tanggal: "2021-12-10T12:45:30.000Z",
        tahun: "2021",
        jumlah: 122100,
        __v: 0,
      }
    ];
    
    
    let newObj = {
      data: [{
        persediaanName: "beras_merah",
        jumlah: 0
      },{
        persediaanName: "beras_putih",
        jumlah: 0
      }],
      total: 0
    };
    
    data.map(eachObj => {
      if (eachObj.persediaanName === 'beras_putih') {
        newObj.data[1].jumlah += eachObj.jumlah;
      } else {
        newObj.data[0].jumlah += eachObj.jumlah;
      }
    });
    
    newObj.total += newObj.data[0].jumlah + newObj.data[1].jumlah;
    
    console.log('New Obj ==>', newObj);