【问题标题】:How to get total of same key values using promises?如何使用承诺获得相同键值的总数?
【发布时间】:2018-10-18 04:01:28
【问题描述】:

我有一个包含两个不同值的对象数组,并且我想要根据键值总计或这些值。我怎样才能让它正常运行?请提前帮助和感谢。

var GetFinancial =  function() {
    var promises = [];
    fnancialObj = {};
    /* calculate total for Firsr*/
    let productAdsPaymentEventListArr = [{ "CurrencyAmount": "300" },{ "CurrencyAmount": "200"} ]
    let productAdsTotal = 0;
    productAdsPaymentEventListArr.forEach(function(productAdsPaymentEventListItem, index) {
        let valueType = 'productAdsPaymentTotal'
        promises.push(GetFinancialEventWithTotal(productAdsPaymentEventListItem.CurrencyAmount, productAdsTotal, fnancialObj, valueType))
    })
    /* calculate total of second*/
    let productAdsPaymentEventListArr2 = [{ "CurrencyAmount": "30"},{ "CurrencyAmount": "20"} ]
    let productAdsTotal2 = 0;
    productAdsPaymentEventListArr2.forEach(function(productAdsPaymentEventListItem2, index) {
        let valueType = 'productAdsPaymentTotal2'
        promises.push(GetFinancialEventWithTotal(productAdsPaymentEventListItem2.CurrencyAmount, productAdsTotal2, fnancialObj, valueType))
    })
    Promise.all(promises).then(function(result) {
        console.log("product update or inserted successfully in all ", result)
        resolve(result)
    }).catch(function(err) {
        console.log("err in update or inserted in all promise", err)
    })
}

Promice 定义在这里:

var GetFinancialEventWithTotal = function(chargeComponent, totalCharge, fnancialObj, objectKey) {
    return new Promise(function(resolve, reject) {
        totalCharge = totalCharge + parseFloat(chargeComponent);
        if (totalCharge) {
            fnancialObj[objectKey] = totalCharge;
            resolve(fnancialObj);
        } else {
            reject("There an Error")
        }
    })
}

我想要这样的输出(每个数组的每个值根据键值相加):

fnancialObj={
    productAdsPaymentTotal : 500,
    productAdsPaymentTotal2 :50,
}

【问题讨论】:

  • 但是你覆盖了函数GetFinancialEventWithTotal。我不明白!
  • 根据@Ele 的观察,看起来第二个GetFinancialEventWithTotal 并没有做任何异步操作。你为什么要把它包装在一个承诺中?
  • 我不完全确定您是否需要基于该代码的 any 承诺。 forEachs 是同步的,第二个 GetFinancialEventWithTotal 函数也是如此。
  • 这是一个方法,它会返回promise。 Promise 定义如下。
  • @VishnuChauhan 我们看到了 Promise 的定义,但不清楚为什么要使用 Promise。为什么不直接返回值?

标签: javascript node.js meteor promise es6-promise


【解决方案1】:

除非您的工作流程中存在异步内容,否则您不需要任何 Promise。对于您当前的问题,您只需在对象数组中添加数量。这就是reduce() 的用途。在每个数组上调用 reduce() 以获取总和并返回包含两个结果的对象。

var GetFinancial =  function() {

    /* calculate total for Firsr*/
    let productAdsPaymentEventListArr = [{ "CurrencyAmount": "300" },{ "CurrencyAmount": "200"} ]
    let productAdsTotal = productAdsPaymentEventListArr.reduce((total, current) => total + parseInt(current.CurrencyAmount), 0);

    /* calculate total of second*/
    let productAdsPaymentEventListArr2 = [{ "CurrencyAmount": "30"},{ "CurrencyAmount": "20"} ]
    let productAdsTotal2 = productAdsPaymentEventListArr2.reduce((total, current) => total + parseInt(current.CurrencyAmount), 0);

    return {
        productAdsPaymentTotal : productAdsTotal,
        productAdsPaymentTotal2 :productAdsTotal2,
    }
    
}
let financials = GetFinancial()
// do any further calculations you want with financials 
console.log(financials)

【讨论】:

  • 您可以将reduce逻辑提取到函数中以供重用。 function sum(array, keyToSum) 之类的东西;然后拨打sum(productAdsPaymentEventListArr, 'CurrencyAmount')
猜你喜欢
  • 2022-08-09
  • 2021-07-23
  • 2022-12-17
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多