简体   繁体   中英

Data Mapping In Typescript

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?
安慰

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 }
        ];
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM