统一数组中的对象,获取总计

问题描述 投票:0回答:1

我正在努力解决这个问题。

我正在开发一个带有条形码扫描仪的拣选应用程序。我有一个多步骤表单,一次显示一种产品。用户可以选择该商品并转到下一件商品,也可以因为没有库存而跳过该商品。 我现在有了这个对象数组,是在提交表单后获得的。它显示用户输入的代码,并使用产品的 id 对其进行验证(我创建了 ids)。如果 code: "" 为空,那是因为用户跳过了它。

[
  {
    "orderid": "12696",
    "id": 3929,
    "name": "Eucaliptus 90 Gr",
    "quantity": 3,
    "code": "3929"
  },
  {
    "orderid": "12696",
    "id": 3929,
    "name": "Eucaliptus 90 Gr",
    "quantity": 3,
    "code": "3929"
  },
 {
    "orderid": "12696",
    "id": 3929,
    "name": "Eucaliptus 90 Gr",
    "quantity": 3,
    "code": ""
  },
 {
    "orderid": "12696",
    "id": 2739,
    "name": "Hellmanns 232 Ml",
    "quantity": 4,
    "code": "2739"
  },
  {
    "orderid": "12696",
    "id": 2739,
    "name": "Hellmanns 232 Ml",
    "quantity": 4,
    "code": "2739"
  },
  {
    "orderid": "12696",
    "id": 2739,
    "name": "Hellmanns 232 Ml",
    "quantity": 4,
    "code": ""
  },
  {
    "orderid": "12696",
    "id": 2739,
    "name": "Hellmanns 232 Ml",
    "quantity": 4,
    "code": ""
  }
]

我正在寻找总数,同一产品有多少件商品被挑选,哪些没有。

这是我需要的一个例子:

[
 {
   "orderid": "12696",
   "id": 3929,
   "name": "Eucaliptus 90 Gr",
   "quantity": 3,
   "picked": 2,
   "notPicked": 1
 },
 {
   "orderid": "12696",
   "id": 2739,
   "name": "Hellmanns 232 Ml",
   "quantity": 4,
   "picked": 2,
   "notPicked": 2
 }
] 

提前致谢!

这是我的代码:

// Here i grab all the code inputs, even empty ones
const totalItemsPickedArr = req.body.code;

//I convert that array of codes into an array of objects    
let newTotalItemsArr = totalItemsPickedArr.map(code => {
        return ({
            code
        })
    })


// I bring all the items from the order via sequelize
db.Detallepedido.findAll({
            where: {
                pedido_id: orderid
            },
            include: [{ all: true, nested: true }],
            order: [['productoorden', 'orden', 'ASC']]
})
.then(products => {
// Here i map the promise and make a new array of objects with the data i need.

let productListArr = products.map(product => {
                    return ({
                        orderid: orderid,
                        id: product.producto_id,
                        name: product.producto_nombre,
                        price: product.precio,
                        quantity: product.cantidad,
                        order: product.productoorden.orden,
                        category:
product.productoorden.categoriaproducto[0].categoria_id,
                        
                    })
                })
// Flattened makes each object multiply by its quantity, so it returns a bigger array, like the one i showed in the first array.
const flattened = productListArr.reduce((acc, item) => {
                    return [
                        ...acc,
                        ...Array.from({ length: item.quantity }, () => ({...item}))
                        
                    ]
                }, [])

//then i make a new array with the flattened array and I paste the code the user input.
let newProductListArr = flattened.map((product, index) => {
                    return ({
                        ...product, 
                        ...newTotalItemsArr[index]})
                })

在展平数组后,我只需要为每个产品显示一个对象,并根据输入的代码添加已挑选/未挑选的总数。

抱歉,如果它太复杂,我知道这不是最好的代码。谢谢!

javascript node.js sequelize.js
1个回答
1
投票

我根据您发布的数组和预期的数组结果添加此方法。

我没有详细介绍整个代码,只是想指出如何执行转换。

let scans = [{...}] // all of the items

let groups = scans.reduce((acc, cur) => {
  let { id, code } = cur
  let { picked, notPicked } = acc[id] || { picked: 0, notPicked: 0 }

  // perform operations to count modifying the variables
  if (code === '')
    notPicked++
  else
    picked++

  acc[id] = { ...cur, picked, notPicked }
  return acc
}, {})

let converted = Object.values(groups)
© www.soinside.com 2019 - 2024. All rights reserved.