-2

I have an array of objects that looks like this:

var model = ([{ "MKT_BRAND_NAME": "LUX", "Brand_GRP": 783.52, "Category_GRP": 0.0, "SOV": 0.051, "MM": "10", "QUARTER": "", "YYYY": "2014", "TargetGroupId": null  },
           { "MKT_BRAND_NAME": "LUX", "Brand_GRP": 356.12, "Category_GRP": 0.0, "SOV": 0.050, "MM": "11", "QUARTER": "", "YYYY": "2014", "TargetGroupId": null },
           { "MKT_BRAND_NAME": "LUX", "Brand_GRP": 935.27, "Category_GRP": 0.0, "SOV": 0.23, "MM": "7 ", "QUARTER": "", "YYYY": "2014", "TargetGroupId": null},
           { "MKT_BRAND_NAME": "LUX", "Brand_GRP": 3380.81, "Category_GRP": 0.0, "SOV": 0.15, "MM": "8 ", "QUARTER": "", "YYYY": "2014", "TargetGroupId": null },
           { "MKT_BRAND_NAME": "LUX", "Brand_GRP": 3536.21, "Category_GRP": 0.0, "SOV": 0.15, "MM": "9 ", "QUARTER": "", "YYYY": "2014", "TargetGroupId": null}
           { "MKT_BRAND_NAME": "PEARS", "Brand_GRP": 3380.81, "Category_GRP": 0.0, "SOV": 0.18, "MM": "8 ", "QUARTER": "", "YYYY": "2014", "TargetGroupId": null },
           { "MKT_BRAND_NAME": "PEARS", "Brand_GRP": 3536.21, "Category_GRP": 0.0, "SOV": 0.17, "MM": "9 ", "QUARTER": "", "YYYY": "2014", "TargetGroupId": null}]);

I want to display SOV and MM values for a particular MKT_BRAND_NAME. I want to assign MKT_BRAND_NAME to "key" and MM and SOV pair to "values".Something like this

  var model = [{
            "key": "Lux",
            "values": [{MM:"10",SOV:0.015},
                       {MM:"11",SOV:0.050},
                       {MM:"7",SOV:0.23},
                       {MM:"8",SOV:0.15},
                       {MM:"9",SOV:0.15}]
                    }, {
           "key": "Pears",
           "values": [{MM:"8",SOV:0.018},
                      {MM:"9",SOV:0.017},

        }];

Can any one please tell me how to achieve this through for loop in JavaScript.

softvar
  • 17,917
  • 12
  • 55
  • 76
Kalpana Gupta
  • 41
  • 1
  • 8
  • Possible duplicate of http://stackoverflow.com/questions/19529403/javascript-loop-through-object-array – softvar Nov 29 '14 at 11:28
  • This question appears to be off-topic because it is yet another "manipulate my JS object for me" question with no apparent effort on the part of the OP. –  Nov 29 '14 at 11:39
  • Use Underscore's `_.groupBy`. –  Nov 29 '14 at 11:39
  • @ torazaburo.U are correct.I not able to group the MKT_BRAND_NAME.Can u tell me how can I implement _.groupBy in below code. – Kalpana Gupta Dec 01 '14 at 08:08

2 Answers2

2

This function does what you want:

function convert(model) {
    var new_model = [];
    for (var i = 0; i < model.length; i++) {
        var model_record = model[i];
        var matched_new_model_record = null;
        for (var j = 0; j < new_model.length; j++) {
            var new_model_record = new_model[j];
            if (new_model_record.key === model_record.MKT_BRAND_NAME) {
                matched_new_model_record = new_model_record;
                break;
            }
        }
        if (matched_new_model_record === null) {
            matched_new_model_record = {key: model_record.MKT_BRAND_NAME, values: []};
            new_model.push(matched_new_model_record);
        }
        matched_new_model_record.values.push({MM: model_record.MM, SOV: model_record.SOV});
    }
    return new_model;
}
Soroush
  • 1,055
  • 2
  • 18
  • 26
  • @Soroush.I am not able to group the MKT_BRAND_NAME.When I am passing multiple values for MKT_BRAND_NAME,code is returning MM,SOV values for each and individual MKT_BRAND_NAME.So can you tell me how can I group MKT_BRAND_NAME since I have 5 values for LUX and 2 for pears. – Kalpana Gupta Dec 01 '14 at 08:36
  • @KalpanaGupta, what do you exactly mean by _grouping_ the MKT_BRAND_NAME? – Soroush Dec 03 '14 at 10:01
0

How about using the reduce function like so:

grouped = model.reduce(function(memo, o){

  if (typeof(memo[o["MKT_BRAND_NAME"]]) == "undefined")
    memo[o["MKT_BRAND_NAME"]] = [];

  memo[o["MKT_BRAND_NAME"]].push({MM: o["MM"], SOV: o["SOV"]});

  return memo;

}, {});

result = [];
for (var key in grouped) {
  result.push({key: key, values: grouped[key]})
}

console.log(JSON.stringify(result, null, 4))
Furqan Zafar
  • 1,471
  • 1
  • 13
  • 17