【发布时间】:2022-01-15 04:30:40
【问题描述】:
我有一个对象数组,如下我想返回一个包含数据对象和总数的数据对象 从数据中返回一个对象数组并显示数据总和“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,
},
];
像这样返回对象数据和总计。 jumlah:来自 beras_merah 和 piutang beras_merah 的 sum penjualan。 total : 数据 jumlah beras_merah 和 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"
}
【问题讨论】:
-
您能否向我们展示您迄今为止所做的尝试。您会发现通过尝试可以学到更多知识,而不是有人只是编写代码,然后人们会展示它是否可能出错了。首先,像
Array.reduce这样的问题似乎很合适。 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
标签: javascript arrays object sum