0

I have the below js see this fiddle.

    var name = ["Ted", "Sarah", "Nancy", "Ted", "Sarah", "Nancy"];
    var prodID = [111, 222, 222, 222, 222, 222];
    var prodName = ["milk", "juice", "juice", "juice", "juice", "juice"];
    var qty = [1, 2, 3, 3, 3, 1];
    var data = {};
    for (i = 0; i < name.length; i++) {
        if (!data[name[i]]) {
            purchased = {
                "name": name[i],
                "prodID": prodID[i],
                "prodName": prodName[i],
                "qty": qty[i]
            };
            data[name[i]] = purchased;
        } else {
            purchased = data[name[i]];
            purchased.qty += +qty[i];
        }
        data[name[i]] = purchased;
    }
    data = $.map(data, function (val, key) {
        return val;
    });
    data.sort();
    $.each(data, function (i, val) {
        var plural = "";
        if (val.qty > 1) {
            plural = "'s";
        }
    $('body').append(val.name + ' has purchased ' + val.qty + ' bottle' + plural + ' of ' + val.prodName + '<br>');
    });

Which outputs

Ted has purchased 4 bottle's of milk
Sarah has purchased 5 bottle's of juice
Nancy has purchased 4 bottle's of juice

What I want to do is show each of the products they have purchased and how many of each.

eg looking at the above Ted has purchased 1 milk and 3 bottle's of juice, but I am currently outputing Ted has purchased 4 bottle's of milk as 4 is the total qty of his purchases.
How can I group it by each prodID/ProdName so that I get the below.

Ted has purchased 1 bottle of milk and 3 bottle's of juice
Sarah has purchased 5 bottle's of juice
Nancy has purchased 4 bottle's of juice

3
  • 3
    your data structure seems a little odd. Is there a reason you're using 4 arrays and linking the data by index? An object would be much better suited to this. Commented Jun 10, 2014 at 9:14
  • thanks for the feedback, I will look into this, if I console.log(purchased) in the for loop it tells me I am getting an object? am I misinterpreting what I am seeing in the console.log here? Commented Jun 10, 2014 at 9:31
  • 1
    @ak85: purchased is indeed an object. The remark was about the initial 4 arrays : you would be better off using one single array holding objects. Commented Jun 10, 2014 at 10:16

1 Answer 1

2

The first step is to convert the data structure to something more appropriate, for example:

name.forEach(function(nam, i) {
    data[nam] = data[nam] || {};
    pid = prodID[i];
    data[nam][pid] = data[nam][pid] || {total:0, pname: prodName[i]};
    data[nam][pid].total += qty[i];
});

which gives you

 "Ted": {
  "111": {
   "total": 1,
   "pname": "milk"
  },
  "222": {
   "total": 3,
   "pname": "juice"
  }
 },
 "Sarah": {
  "222": {
   "total": 5,
   "pname": "juice"
  }
 },
 "Nancy": {
  "222": {
   "total": 4,
   "pname": "juice"
  }
 }

Given this, generating the output is pretty straightforward:

message = Object.keys(data).map(function(nam) {
    var purchases = data[nam];
    return nam + ' got ' + Object.keys(purchases).map(function(pid) {
        return purchases[pid].total + ' bottle(s) of ' + purchases[pid].pname
    }).join(' and ');
}).join(' <br> ');

http://jsfiddle.net/yP6X5/1/

(pluralization left as an exercise, do note however that the correct form is bottles, not bottle's).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.