How can I use Papa Parse or Angular to merge objects based on a certain attribute?

682 views Asked by At

I'm using Papa Parse to convert a CSV file with name, date and result to JSON objects, but I want objects based on the names instead of the rows of the CSV file. If this isn't possible using Papa Parse, is there a method within Angular (or an Angular package) that I can use to do this?

I get the following output:

[
    {
        name: 'foo',
        date: '11-10-2016',
        result: '10'
    },
    {
        name: 'foo',
        date: '12-10-2016',
        result: '8'
    },
    {
        name: 'bar',
        date: '11-10-2016',
        result: '1'
    },
    {
        name: 'bar',
        date: '15-10-2016',
        result: '4'
    }
]

But I want this output:

[
    {
        name: 'foo',
        results: 
        [
            {
                date: '11-10-2016',
                result: '10'
            },
            {
                date: '12-10-2016',
                result: '8'
            }
        ]
    },
    {
        name: 'bar',
        results: 
        [
            {
                date: '11-10-2016',
                result: '1'
            },
            {
                date: '15-10-2016',
                result: '4'
            }
        ]
    }
]

I found an answer how to do it with plain javascript here, but if there's a function to do it within Papa Parse or Angular I would prefer to use that.

1

There are 1 answers

0
pokoli On BEST ANSWER

Papaparse is designed to only parse data but not manipulate it, so there is (and I'm wondering that it will never be) a function for aggregating the parsed data.

Having said that, I think that using the plain javascript function after parsing the data will be enough to achieve what you want.