2

I have a raw data like this:

{
    "Team1": [
        {
            "priority": "P0",
            "status": "Open",
            "teamName": "Team1"
        },
        {
            "priority": "P1",
            "status": "Closed",
            "teamName": "Team1"
        },
        {
            "priority": "P0",
            "status": "Closed",
            "teamName": "Team1"
        }
    ],
    "Team2": [
        {
            "priority": "P1",
            "status": "Open",
            "teamName": "Team2"
        }
    ]
}

I would like to transform the data as follows:

{
    "Team1": {
        "teamName": "Team1",
        "Open": {
            "P0": 1,
            "P1": 0
        },
        "Closed": {
            "P0": 1,
            "P1": 1
        }
    },
    "Team2": {
        "teamName": "Team2",
        "Open": {
            "P0": 0,
            "P1": 1
        },
        "Closed": {
            "P0": 0,
            "P1": 1
        }
    }
}

I'm trying with Underscore.js, but not very successful since I'm novice in this.

I am till here:

_.groupBy(data, function(element) {
    element.teamName + '#' + element.status
});

But I'm clueless on how to proceed.

4
  • This isn't a code writing service. Show what you tried so people can help fix code you wrote Commented Sep 20, 2016 at 13:46
  • Have you tried anything already? I would say the easiest way would be to simply loop through the original structure, and build the new structure from that. It shouldn't be to hard for you to give it a go. Commented Sep 20, 2016 at 13:46
  • @charlietfl: Thanks for the advise. I accept SO is not a code writing service, at least, it will help me to learn something new. I've learnt lot from SO and gave back to the community as far as I can. This is something which I don't even know how to proceed, that is why I'm here. I'm here to find a way. IMHO. Commented Sep 20, 2016 at 14:00
  • @DBS: Yes, if I don't find any way to perform this using Underscore, then I'm only left with that option. Commented Sep 20, 2016 at 14:02

1 Answer 1

1

You could first iterate the object's keys and the arrays and get the included properties and then the same for the count.

var data = { "Team1": [{ "priority": "P0", "status": "Open" }, { "priority": "P1", "status": "Closed" }, { "priority": "P0", "status": "Closed" }], "Team2": [{ "priority": "P1", "status": "Open" }] },
    keys = Object.keys(data),
    priorities = new Set(),
    grouped = {};

keys.map(k => data[k].forEach(a => priorities.add(a.priority)));

keys.map(k => {
    grouped[k] = grouped[k] || {};
    data[k].forEach(a => {
        grouped[k][a.status] = grouped[k][a.status] || [...priorities].reduce((r, b) => (r[b] = 0, r), {});
        grouped[k][a.status][a.priority]++;
    });
});

console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.