0

I'm trying to use mapreduce to calculate monthly sales and graph with chart.js later.

Suppose I have a JSON response with fields, amount and date.

For my map function, I took out the month:

 month = _.map([items[i].transactionDate], function(date) { 
     return {
         lables: items[i].transactionDate.slice(5,7)
     }; 
});

and for my reduce function ... well I didn't know what to do here.

  sum = _.reduce([items[i].amt], function(memo, num) {
      return memo + items[i].amt
  });

I realized that this will eventually calculate total sum not per month. Problem is that I don't know how to relate the two functions properly.

Edit: Per request, my JSON :

 "items": [
 {
"_id": "56d1cf3704aee3c68d89cc09",
"amt": 5,
"transactionDate": "2016-02-27T16:30:47.561Z",
 }
]

and what I'm trying to get out of this is sales per month on a graph. so far I've been able to project months and total sale but not sales per a specific month.

3
  • 1
    can you show you JSON? Commented Apr 22, 2016 at 6:30
  • 1
    Can you be more explicit about how the data looks like? Maybe a small section of data format. Also what exactly are you looking for in the final output? Commented Apr 22, 2016 at 6:30
  • 2
    The JSON you have provided and the snippet you provided doesnt seem to gel in your snippet you have transactionDate but in json there is nothing like that.. Commented Apr 22, 2016 at 6:41

4 Answers 4

1

I think, simply make one loop with adding new variable

let result= {};

_.forEach(items, (item) => {
    // get month
    let month = item.transactionDate.slice(5,7);
    // get amount
    let amount = item.amt;

    // check if we have this month in finaly object
    if(month in finaly) {
       // added amount
       result[month] += amount;
    } else {
       // if this month doesn't exist added with inital value
       result[month ] = amount;
    }
});

When you can get all amount of certain month or get sum of all months

let allSum = _.reduce(result, (sum, amount) => sum += amount);
let amountInCertainMonth = result["01"];
Sign up to request clarification or add additional context in comments.

Comments

1

map and reduce are functions of Array that iterate over the array for you

map returns a new array with the results of the mapping function for each element

var months = items.map ( item => item.transactionDate.slice(5,7) )

and reduce applies the reducing function to each element and an accumulator.

var sum = items.reduce( (accum, item) => accum + item.amt , 0);

Comments

0

I assume items[i] is an object that has .transactionsDate and .amt as two arrays with corresponding indices. If so, you can get sales per month and total sales within one reduce function by starting with { totalSales, months }.

var chartData = items[i].transactionDate.reduce(function(total, month, index) {
    var monthlyData = {
        label: month.slice(5, 7),
        sales: items[i].amt[index]
    };
    total.months.push(monthlyData);
    total.totalSales += item.amt;
    return total;
}, { 
    totalSales: 0, months: []
});

// get total sales
var totalSales = chartData.totalSales;
// get sales for May starting at index 0
var salesForMay = chartdata.months[4];

Let me know if this is what you were looking for.

Comments

0

I know this is old, but here is how to use reduce as a group by

    var results = items
        .map(function(data){
            return {"month": data.transactionDate.slice(0,7), "amt": data.amt};
        })
        .reduce(function(amounts, data){            
            if (!amounts.hasOwnProperty(data.month))
                amounts[data.month] = data.amt;
            else
                amounts[data.month] = amounts[data.month] + data.amt;

            return amounts;
	
        }, {});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.