【问题标题】:Javascript reduce gives incorrect valuesJavascript reduce 给出不正确的值
【发布时间】:2013-08-03 23:21:25
【问题描述】:

认为数据是

{
    "Groceries": [
        {
            "category": {
                "uri": "/categories/20fe3330-80e1-4908-9f57-5b7ef575b197",
                "parent": "Food & Drink",
                "name": "Groceries"
            },
            "amount": "85.14",
            "debit": true
        },
        {
            "category": {
                "uri": "/categories/20fe3330-80e1-4908-9f57-5b7ef575b197",
                "parent": "Food & Drink",
                "name": "Groceries"
            },
            "amount": "19.15",
            "debit": true
        },
        {
            "category": {
                "uri": "/categories/20fe3330-80e1-4908-9f57-5b7ef575b197",
                "parent": "Food & Drink",
                "name": "Groceries"
            },
            "amount": "4.2",
            "debit": true
        },
        {
            "category": {
                "uri": "/categories/20fe3330-80e1-4908-9f57-5b7ef575b197",
                "parent": "Food & Drink",
                "name": "Groceries"
            },
            "amount": "16.08",
            "debit": true
        },
        {
            "category": {
                "uri": "/categories/20fe3330-80e1-4908-9f57-5b7ef575b197",
                "parent": "Food & Drink",
                "name": "Groceries"
            },
            "amount": "28.48",
            "debit": true
        },
        {
            "category": {
                "uri": "/categories/20fe3330-80e1-4908-9f57-5b7ef575b197",
                "parent": "Food & Drink",
                "name": "Groceries"
            },
            "amount": "35.82",
            "debit": true
        },
        {
            "category": {
                "uri": "/categories/20fe3330-80e1-4908-9f57-5b7ef575b197",
                "parent": "Food & Drink",
                "name": "Groceries"
            },
            "amount": "12.15",
            "debit": true
        },
        {
            "category": {
                "uri": "/categories/20fe3330-80e1-4908-9f57-5b7ef575b197",
                "parent": "Food & Drink",
                "name": "Groceries"
            },
            "amount": "4.19",
            "debit": true
        },
        {
            "category": {
                "uri": "/categories/20fe3330-80e1-4908-9f57-5b7ef575b197",
                "parent": "Food & Drink",
                "name": "Groceries"
            },
            "amount": "34.11",
            "debit": true
        },
        {
            "category": {
                "uri": "/categories/20fe3330-80e1-4908-9f57-5b7ef575b197",
                "parent": "Food & Drink",
                "name": "Groceries"
            },
            "amount": "3.36",
            "debit": true
        },
        {
            "category": {
                "uri": "/categories/20fe3330-80e1-4908-9f57-5b7ef575b197",
                "parent": "Food & Drink",
                "name": "Groceries"
            },
            "amount": "11.32",
            "debit": true
        }
    ],
    "Restaurants": [
        {
            "category": {
                "uri": "/categories/15147702-8227-4ee8-8b05-d2e8d532bd0a",
                "parent": "Food & Drink",
                "name": "Restaurants"
            },
            "amount": "18.43",
            "debit": true
        }
    ]
} 

我希望这些数据类似于

{
    "Groceries": 1234.12, # (1234.12 is madeup value for now)added values for all Groceries transaction
    "Restaurents": 18.42
}

我正在使用Lodash 来执行此操作,我的代码看起来像

var mapped = _.reduce(data, function(result, num, key){
  var sum = 0.0;
  sum = _.reduce(num, function(sum, n){
      console.log(key + ':' + parseFloat(n.amount));
      return sum + parseFloat(n.amount);
  });
  result[key] = sum;
  return result;
}, {})

我得到的结果是

"{
    "Groceries": "[object Object]19.154.216.0828.4835.8212.154.1934.113.3611.32",
    "Restaurants": {
        "category": {
            "uri": "/categories/15147702-8227-4ee8-8b05-d2e8d532bd0a",
            "parent": "Food & Drink",
            "name": "Restaurants"
        },
        "amount": "18.43",
        "debit": true
    }
}"

我在这里做错了什么?

【问题讨论】:

    标签: javascript lodash


    【解决方案1】:

    来自docs

    将集合缩减为一个值,该值是通过回调运行集合中每个元素的累积结果,其中每个后续回调执行都会消耗上一次执行的返回值。如果没有传入累加器,则将集合的第一个元素作为累加器的初始值

    因此,您应该提供一个初始值或解析 sum,因为它是一个 String 对象,我建议您提供一个初始值,因为每次过去都解析它是没有意义的。

    所以你可以这样做:

    var s = _.reduce(num, function(sum, n){
      return sum + parseFloat(n.amount);
    }, 0);
    

    【讨论】:

      【解决方案2】:
      var restaurants = {
          "Restaurants": [
              {
                  "category": {
                      "name": "Restaurants"
                  },
                  "amount": "88.91"
              },
              {
                  "category": {
                      "name": "Restaurants"
                  },
                  "amount": "58.14"
              }]};
      
      var groceries = {
          "Groceries": [
              {
                  "category": {
                      "name": "Groceries"
                  },
                  "amount": "58.41"
              },
              {
                  "category": {
                      "name": "Groceries"
                  },
                  "amount": "85.14"
              }]};
      
      function reduce_category(collection) {
          return _.reduce(collection, function (memo, entry) { 
              return memo += parseFloat(entry.amount, 10);
          }, 0);
      }
      
      console.log((function reduce_collection(collection) {
          var sum = _.reduce(collection, function (memo, entry) {
              return memo += reduce_category(entry[_.first(_.keys(entry))]);
          }, 0);
      
          return (Math.round(sum * 100) / 100).toFixed(2);
      }([restaurants, groceries]))) // => 290.60
      

      Fiddle.

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-19
        • 2018-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-05
        • 1970-01-01
        相关资源
        最近更新 更多