【问题标题】:sum array and return object javascript [duplicate]求和数组并返回对象javascript [重复]
【发布时间】: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


【解决方案1】:

这是一种粗略的做事方式:您可以定义输出模型并分配初始值(在本例中为 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);

【讨论】:

    猜你喜欢
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多