Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

120
Visualizações
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"
}
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

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);

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda