React实现的问题(将两个项目合并为一个值)

huangapple go评论58阅读模式
英文:

React problem with implementation (two items to one value)

问题

我尝试获得类似购物车的东西,所以值将按数量计算,我已经"修改"了类似这样的东西,但这不起作用:

我将从API响应开始:

"magazyn": [
  {
    "productId": 1,
    "productGUID": "491a8965545a4071bda2811094cf55d1",
    "productSKU": 10,
    "productTitle": "aaaaaa",
    "productCategory": "string",
    "productDescription": "string",
    "productQuantity": 1,
    "productVatRate": 1,
    "productNettoPurchasePrice": 1,
    "productNettoSellingPrice": 1,
    "productNettoSalesMargin": 1,
    "productBruttoSellingPrice": 1,
    "productNettoIncomeValue": 1,
    "productVolume": "Szt"
  },
  {
    "productId": 2,
    "productGUID": "21a63a6a0ff84cb0b1b476284ffe7368",
    "productSKU": 13,
    "productTitle": "absddad",
    "productCategory": "string",
    "productDescription": "string",
    "productQuantity": 2,
    "productVatRate": 2,
    "productNettoPurchasePrice": 2,
    "productNettoSellingPrice": 2,
    "productNettoSalesMargin": 2,
    "productBruttoSellingPrice": 38,
    "productNettoIncomeValue": 2,
    "productVolume": "Szt"
  }
],
"productId": [
  {
    "pcID": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
    "productId": 1,
    "productQuantity": 5
  },
  {
    "pcID": "6b29fc40-ca47-1067-b31d-00dd010662da",
    "productId": 2,
    "productQuantity": 4
  }
]
{item.productId.map(val => (
 item.magazyn.map((item2) => item2.productBruttoSellingPrice).reduce((previousValue, currentValue2) => {
return previousValue + currentValue2 * val.productQuantity;
}, 0)
))}

我也尝试过以下代码:

{item.productId.map((val) => val.productQuantity).reduce((firstVal, nextVal) => {
 item.magazyn.map((item2) => item2.productBruttoSellingPrice).reduce((previousValue, currentValue2) => {
return previousValue + currentValue2 * val;
}, 0)
},0)
)}

但第一个给出了奇怪的值,例如当结果是30时,这给了我3030,第二次尝试什么也没给出:P

我的问题是,magazyn给我产品价格,但"productId"对象给我一个数量,我想合并它们,就像当productId.productId == magazyn.productId时,productQuantity * productBruttoSellingPrice,并且这将在循环中进行,因为"productId"对象中有不同的项目。

英文:

Im trying to get something like cart, so the values will be counted times quantity, ive "modded" something like this, but this doesnt work:

I will start with the API response:

    "magazyn": [
      {
        "productId": 1,
        "productGUID": "491a8965545a4071bda2811094cf55d1",
        "productSKU": 10,
        "productTitle": "aaaaaa",
        "productCategory": "string",
        "productDescription": "string",
        "productQuantity": 1,
        "productVatRate": 1,
        "productNettoPurchasePrice": 1,
        "productNettoSellingPrice": 1,
        "productNettoSalesMargin": 1,
        "productBruttoSellingPrice": 1,
        "productNettoIncomeValue": 1,
        "productVolume": "Szt"
      },
      {
        "productId": 2,
        "productGUID": "21a63a6a0ff84cb0b1b476284ffe7368",
        "productSKU": 13,
        "productTitle": "absddad",
        "productCategory": "string",
        "productDescription": "string",
        "productQuantity": 2,
        "productVatRate": 2,
        "productNettoPurchasePrice": 2,
        "productNettoSellingPrice": 2,
        "productNettoSalesMargin": 2,
        "productBruttoSellingPrice": 38,
        "productNettoIncomeValue": 2,
        "productVolume": "Szt"
      }
    ],
    "productId": [
      {
        "pcID": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
        "productId": 1,
        "productQuantity": 5
      },
      {
        "pcID": "6b29fc40-ca47-1067-b31d-00dd010662da",
        "productId": 2,
        "productQuantity": 4
      }
    ]
{item.productId.map(val => (
 item.magazyn.map((item2) => item2.productBruttoSellingPrice).reduce((previousValue, currentValue2) => {
return previousValue + currentValue2 * val.productQuantity;
}, 0)
))}

I also tried with

{item.productId.map((val) => val.productQuantity).reduce(firstVal, nextVal) => {
 item.magazyn.map((item2) => item2.productBruttoSellingPrice).reduce((previousValue, currentValue2) => {
return previousValue + currentValue2 * val;
}, 0)
},0)
)}

But the first one gives weird values like, for example when the result is 30 this gives me 3030, the second attempt is gives nothing React实现的问题(将两个项目合并为一个值)

my problem is that, the magazyn give me the product price, but the "productId" object gives me a quantity, i want to merge it like when productId.productId == magazyn.productId then productQuantity * productBruttoSellingPrice and this one will be in the loop because we have different items in "productId" object.

答案1

得分: 1

你能尝试一下以下内容吗?我会解释你的期望吗?

const magazyn = [...item.magazyn];
const productId = [...item.productId];

let sum = 0;
magazyn.forEach((value1) => {
  productId.forEach((value2) => {
    if (value1.productId === value2.productId){
        sum= sum + 
value1.productBruttoSellingPrice * 
value2.productQuantity
    } 
  });
});

console.log(sum);
英文:

Could you try the following? Do I interpret what you are expecting?

const magazyn = [...item.magazyn];
const productId = [...item.productId];

let sum = 0;
magazyn.forEach((value1) => {
  productId.forEach((value2) => {
    if (value1.productId === value2.productId){
        sum= sum + 
value1.productBruttoSellingPrice * 
value2.productQuantity
    } 
  });
});

console.log(sum);

huangapple
  • 本文由 发表于 2023年3月31日 19:48:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定