求和数组和 return 对象 javascript
sum array and return object javascript
我有一个对象数组如下我想要return一个包含数据对象和总计的数据对象
returns 来自数据的对象数组并显示数据总和“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 这样子。
jumlah:来自 beras_merah 和 piutang beras_merah 的 sum penjualan。
总计:来自数据 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"
}
这是一种粗略的处理方式:您可以定义输出模型并分配初始值(在本例中为 0)并遍历数组并进行必要的操作。
如果 persediaanName
有很多值,那么您可以考虑维护一个包含这些值的数组,然后使用粗略的方法。
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);
我有一个对象数组如下我想要return一个包含数据对象和总计的数据对象 returns 来自数据的对象数组并显示数据总和“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 这样子。 jumlah:来自 beras_merah 和 piutang beras_merah 的 sum penjualan。 总计:来自数据 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"
}
这是一种粗略的处理方式:您可以定义输出模型并分配初始值(在本例中为 0)并遍历数组并进行必要的操作。
如果 persediaanName
有很多值,那么您可以考虑维护一个包含这些值的数组,然后使用粗略的方法。
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);