mardi 26 juillet 2016

JS Object transformation based on values of object properties

I've seen lots of other questions related to js object sorting, most of which tend to suggest using the .map method to sort an object or array of objects based on the value of a property, but I'm trying to achieve something slightly different.

I'm trying to turn this object format:

{
    "commits": [
        {
            "repository": "example-repo-1",
            "commit_hash": "example-hash-1"
        },
        {
            "repository": "example-repo-1",
            "commit_hash": "example-hash-1.2"
        },
        {
            "repository": "example-repo-2",
            "commit_hash": "example-hash-2"
        }
    ]
}

Into an object formatted using the value of 'repository' like this:

{
    "example-repo-1": [
        {
            "repository": "example-repo-1",
            "commit_hash": "example-hash-1"
        },
        {
            "repository": "example-repo-1",
            "commit_hash": "example-hash-1.2"
        }
    ],
    "example-repo-2": [    
        {
            "repository": "example-repo-2",
            "commit_hash": "example-hash-2"
        }
    ]
}

So I need to get my original object, which is an object with an array of other objects, to return an object which contains numerous arrays, named after the values of the repository property and containing each object that matches that property value.

Aucun commentaire:

Enregistrer un commentaire