0

I have two static data sets. I would like to implement them dynamically with an API but I can not format them to the expected type.

This is the first data set:

export let lineChartSeries = [
    {
        name: 'Cumulative',
        series: [
            {
                name: '2022-06-01',
                value: 50
            },
            {
                name: '2022-05-01',
                value: 80
            },
            {
                name: '2022-04-01',
                value: 85
            },
            {
                name: '2022-03-01',
                value: 90
            },
            {
                name: '2022-02-01',
                value: 100
            }
        ]
    }
];

This is the second data set :

export let barChart: any = [
    {
        name: '2022-06-01',
        value: 50000
    },
    {
        name: '2022-05-01',
        value: 30000
    },
    {
        name: '2022-04-01',
        value: 10000
    },
    {
        name: '2022-03-01',
        value: 5000
    },
    {
        name: '2022-02-01',
        value: 500
    }
];

And this is my code for mapping the data.

this.apiDashboard.api("test").toPromise().then((data: any) => {
            this.multi = [];
            this.single = [];
            data.list.forEach(element => {

                this.multi = data.list.map(datum => ({ name: "Cumulative", series: [{ 
                name: datum.period, value: datum.cumulative }] }));
                this.single = data.list.map(datum => ({
                    name: datum.period, value: datum.amount
                }));
            });
        }

This is the console log for results. The first array is the expected result and the second array is mine data format type. I have to put the period and the value inside of the series[]. How could I deal with this data mapping?
Console

Ugur A
  • 23
  • 5
  • what is the final data type you expect and are you trying to replace with current values or add these new values to array? – Tarang Rathod Jun 15 '22 at 13:58
  • you should see this post : [enter link description here](https://stackoverflow.com/questions/40774697/how-can-i-group-an-array-of-objects-by-key) – Dakerl Jun 15 '22 at 14:30
  • I want first array type on the console log. The second is the what I get. – Ugur A Jun 15 '22 at 14:56

1 Answers1

0

I found a reasonable way.

To map data like this: Create an array, map the data on this array and attend it to our data set.

Code:

   let cumulative = [];

    this.apiDashboard.api("month-transacted").toPromise().then((data: any) => {
        this.dataSource = [];
        this.multi = [];
        this.single = [];
        data.list.forEach(element => {

            this.dataSource.push(element);

            cumulative = data.list.map(datum => ({ name: datum.period, value: datum.cumulative }));
            this.single = data.list.map(datum => ({
                name: datum.period, value: datum.amount
            }));
        });

        this.multi = [
            { name: "Cumulative", series: cumulative }
        ];
    });
}
Ugur A
  • 23
  • 5