daydreamer
daydreamer

Reputation: 91957

Javascript reduce gives incorrect values

Consider the data to be

{
    "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
        }
    ]
} 

All I want this data to be in similar to something like

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

I am using Lodash to do this, my code looks like

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;
}, {})

and the result i get is

"{
    "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
    }
}"

What is that I am doing incorrect here?

Upvotes: 3

Views: 1667

Answers (2)

pdoherty926
pdoherty926

Reputation: 10349

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.

Upvotes: 0

Alberto Zaccagni
Alberto Zaccagni

Reputation: 31560

From the docs:

Reduces a collection to a value which is the accumulated result of running each element in the collection through the callback, where each successive callback execution consumes the return value of the previous execution. If accumulator is not passed, the first element of the collection will be used as the initial accumulator value

So you should either provide an initial value or parse sum since it is a String object, I would suggest giving an initial value since it doesn't make sense to parse it each time past the first.

So you could do:

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

Upvotes: 4

Related Questions