1

I have a set of data where i need to format and render in HTML.

This is the data :

var data = [{
        name: 'john',
        age: 20,
        department: 'financial',
        group: 'groupA'
    },
    {
        name: 'john',
        age: 20,
        department: 'healthcare',
        group: 'groupA'
    },
    {
        name: 'john',
        age: 20,
        department: 'insurance',
        group: 'groupA'
    },
    {
        name: 'john',
        age: 20,
        department: 'Realestate',
        group: 'groupB'
    }, {
        name: 'Mark',
        age: 20,
        department: 'financial',
        group: 'groupA'
    },
    {
        name: 'Mark',
        age: 21,
        department: 'insurance',
        group: 'groupA'
    },
    {
        name: 'Mark',
        age: 21,
        department: 'financial',
        group: 'groupB'
    }
];

Here i have multiple users where he belongs to different groups and department.I want to format in such a way the if he belongs to same group then get the department which he belongs to in array and for other department show that as a new object.

The response should look like this:

var data_formatted = [

    [{
            name: 'john',
            age: 20,
            department: ['financial', 'healthcare', 'insurance'],
            group: 'groupA'
        },
        {
            name: 'john',
            age: 20,
            department: 'Realestate',
            group: 'groupB'
        }

    ],

    [{
            name: 'Mark',
            age: 20,
            department: ['financial', 'insurance'],
            group: 'groupA'
        },
        {
            name: 'Mark',
            age: 21,
            department: 'financial',
            group: 'groupB'
        }
    ]


];

Any suggestions Please!

Terry
  • 63,248
  • 15
  • 96
  • 118
  • 2
    JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Sep 27 '17 at 14:10
  • Is the source data generated by you? if so, could you show us how? – Toni Michel Caubet Sep 27 '17 at 14:12
  • I am getting this data from AJAX call from back end – sree3505577 Sep 27 '17 at 14:13
  • 3
    You should probably look into how this Json string is generated, and make changes there. If you don't have access to this code, just use the `$.jsonParse()` function to parse that string into an object and loop over it to create new department lists for each user. – Glubus Sep 27 '17 at 14:13
  • @sree3505577 but you generated it or not? beause formatting the way you want should be done in the back-end – Toni Michel Caubet Sep 27 '17 at 14:15
  • It is not about parsing the JSON it's about grouping and formatting. – sree3505577 Sep 27 '17 at 14:16
  • You're probably better off performing that grouping/coalescing on the server side. For example, if you are using MySQL to generate that output from your API endpoint, `GROUP BY` clause will be helpful. – Terry Sep 27 '17 at 14:16
  • @sree3505577 You need to parse it first, before grouping and formatting. – evolutionxbox Sep 27 '17 at 15:58

1 Answers1

0

I think this may help you.

var data = [{
        name: 'john',
        age: 20,
        department: 'financial',
        group: 'groupA'
    },
    {
        name: 'john',
        age: 20,
        department: 'healthcare',
        group: 'groupA'
    },
    {
        name: 'john',
        age: 20,
        department: 'insurance',
        group: 'groupA'
    },
    {
        name: 'john',
        age: 20,
        department: 'Realestate',
        group: 'groupB'
    }, {
        name: 'Mark',
        age: 20,
        department: 'financial',
        group: 'groupA'
    },
    {
        name: 'Mark',
        age: 21,
        department: 'insurance',
        group: 'groupA'
    },
    {
        name: 'Mark',
        age: 21,
        department: 'financial',
        group: 'groupB'
    }
];

var data_formatted = [];

var byname = _.groupBy(data, function(element){return element.name});
_.forEach(byname, function(element){
  var bygroup = _.groupBy(element, function(element){return element.group});
  _.forEach(bygroup, function(element, key, groups){
    var departments = _.map(element, 'department');
    element[0]['department'] = departments;
    groups[key] = element[0];
  })
  data_formatted.push(_.values(bygroup));
})

console.log(data_formatted);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Alvaro Flaño Larrondo
  • 5,516
  • 2
  • 27
  • 46