'How to create a table with Javascript and/or JQuery dynamicaly from an Object?

I am trying to create a dynamic table with javascript where the values from "A" should be summed if "M" has the same value. In addition, the table header should be created from the values from "Code". I'm sorry that I can't describe it better, but I hope that my example shows what I mean.

var arr = [{
  "M": "52800",
  "Code": "093",
  "A": 1
}, {
  "M": "52800",
  "Code": "050",
  "A": 2
}, {
  "M": "56301",
  "Code": "093",
  "A": 3
}, {
  "M": "57401",
  "Code": "060",
  "A": 1
}, {
  "M": "57401",
  "Code": "090",
  "A": 5
}, {
  "M": "57401",
  "Code": "093",
  "A": 3
}, {
  "M": "57401",
  "Code": "080",
  "A": 5
}];

console.log(arr);

The result table should look like:

M 093 050 060 090 080
52800 1 2
56301 3
57401 3 1 5 5

It is also possibe to create the table if the "Code" and "A" is nested in "CodeHead" like this?

    var arr = [{
        "M": "52800",
        "CodeHead": [{
            "Code": "093",
            "A": 1
        }, {
            "Code": "050",
            "A": 2
        }]

    }, {
        "M": "56301",
        "CodeHead": [{
            "Code": "093",
            "A": 3
        }]
    }, {
        "M": "57401",
        "CodeHead": [{
            "Code": "060",
            "A": 1
        }, {
            "Code": "090",
            "A": 5
        }, {
            "Code": "093",
            "A": 3
        }, {
            "Code": "080",
            "A": 5
        }]
    }];


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source