Q:

Reduce JSON data

I'm having JSON data:
var data = [
    { "Date": "\/Date(1583778600000)\/", "Rate": 1310, "Product": "Prod1" },
    { "Date": "\/Date(1583865000000)\/", "Rate": 1320, "Product": "Prod2" },
    { "Date": "\/Date(1583951400000)\/", "Rate": 1330, "Product": "Prod3" },
    { "Date": "\/Date(1583778600000)\/", "Rate": 1340, "Product": "Prod4" },
    { "Date": "\/Date(1583778600000)\/", "Rate": 1350, "Product": "Prod5" },
    { "Date": "\/Date(1584037800000)\/", "Rate": 1360, "Product": "Prod6" },
    { "Date": "\/Date(1583865000000)\/", "Rate": 1370, "Product": "Prod7" },
    { "Date": "\/Date(1583951400000)\/", "Rate": 1380, "Product": "Prod8" }
];

I want to convert this to something like this:
var result = [
    { "Date": "2020-03-10", "Rate": { 1310: "Prod1", 1340: "Prod4", 1350: "Prod5" } },
    { "Date": "2020-03-11", "Rate": { 1320: "Prod2", 1370: "Prod7" } },
    { "Date": "2020-03-12", "Rate": { 1330: "Prod3", 1380: "Prod8" } },
    { "Date": "2020-03-13", "Rate": { 1360: "Prod6" } }
];

Thank You

A:

var data = [
    { "Date": "\/Date(1583778600000)\/", "Rate": 1310, "Product": "Prod1" },
    { "Date": "\/Date(1583865000000)\/", "Rate": 1320, "Product": "Prod2" },
    { "Date": "\/Date(1583951400000)\/", "Rate": 1330, "Product": "Prod3" },
    { "Date": "\/Date(1583778600000)\/", "Rate": 1340, "Product": "Prod4" },
    { "Date": "\/Date(1583778600000)\/", "Rate": 1350, "Product": "Prod5" },
    { "Date": "\/Date(1584037800000)\/", "Rate": 1360, "Product": "Prod6" },
    { "Date": "\/Date(1583865000000)\/", "Rate": 1370, "Product": "Prod7" },
    { "Date": "\/Date(1583951400000)\/", "Rate": 1380, "Product": "Prod8" }
];

var o = data.reduce((a, b) => {
    b.Date = moment(b.Date).format('YYYY-MM-DD');
    a[b.Date] = a[b.Date] || [];
    a[b.Date].push({ [b.Rate]: b.Product });
    return a;
}, {});

var result = Object.keys(o).map(function (k) {
    return { Date: k, Rate: Object.assign.apply({}, o[k]) };
});

var data = [
    { "Date": "\/Date(1583778600000)\/", "Rate": 1310, "Product": "Prod1" },
    { "Date": "\/Date(1583865000000)\/", "Rate": 1320, "Product": "Prod2" },
    { "Date": "\/Date(1583951400000)\/", "Rate": 1330, "Product": "Prod3" },
    { "Date": "\/Date(1583778600000)\/", "Rate": 1340, "Product": "Prod4" },
    { "Date": "\/Date(1583778600000)\/", "Rate": 1350, "Product": "Prod5" },
    { "Date": "\/Date(1584037800000)\/", "Rate": 1360, "Product": "Prod6" },
    { "Date": "\/Date(1583865000000)\/", "Rate": 1370, "Product": "Prod7" },
    { "Date": "\/Date(1583951400000)\/", "Rate": 1380, "Product": "Prod8" }
];

var o = data.reduce((a, b) => {
    b.Date = moment(b.Date).format('YYYY-MM-DD');
    a[b.Date] = a[b.Date] || [];
    a[b.Date].push({ [b.Rate]: b.Product });
    return a;
}, {});

var result = Object.keys(o).map(function (k) {
    return { Date: k, Rate: Object.assign.apply({}, o[k]) };
});

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

