D
D
dc65k2021-05-02 09:13:40
typescript
dc65k, 2021-05-02 09:13:40

How to properly type the data transformation function?

Hello everyone, I have a data transformation function:

const orders = [
    {
        "date": "2017-10-16 12:07:07",
        "docTypesName": "Приход",
        "docId": 564564867361367,
        "image": "https://www.komus.ru/medias/sys_master/root/hd3/h93/9286922043422.jpg",
        "name": "Молочный Изюм 100",
        "price": 102,
        "quantity": 45,
        "removed": 0
    },
    {
        "date": "2017-10-16 12:07:07",
        "docTypesName": "Приход",
        "docId": 564564867361367,
        "image": "https://mariupolcena.com/files/products/9ff44136e6ccb0afb404ad26f727e67d.jpeg",
        "name": "Русская картошка чедар 50",
        "price": 46.3,
        "quantity": 45,
        "removed": 0
    },
    {
        "date": "2017-10-16 12:07:07",
        "docTypesName": "Расход",
        "docId": 564564867361367,
        "image": "https://mariupolcena.com/files/products/9ff44136e6ccb0afb404ad26f727e67d.jpeg",
        "name": "Русская картошка чедар 50",
        "price": 46.3,
        "quantity": 45,
        "removed": 0
    },
    {
        "date": "2017-11-29 17:26:57",
        "docTypesName": "Расход",
        "docId": 564564867361365,
        "image": "https://www.komus.ru/medias/sys_master/root/hd3/h93/9286922043422.jpg",
        "name": "Молочный Изюм 100",
        "price": 102,
        "quantity": 6,
        "removed": 0
    },
]

interface IOrder {
    date: string,
    docTypesName: string,
    docId: number,
    image: string,
    name: string,
    price: number,
    quantity: number,
    removed: number,
}

interface  IProduct {
    image: string,
    name: string,
    price: number,
    quantity: number,
}

interface IDocument {
    date: string,
    docId: number,
    docTypesName: string,
    products: IProduct[],
}

interface IElement {
    date: string,
    documents: IDocument[]
}

interface IResult {
    '2017-10-16': IElement,
}

function f(orders: IOrder[]) {
    const result: IElement = orders.reduce((accumulator: any, currentValue: IOrder) => {
        const date = currentValue.date.split(' ')[0];

        if (!accumulator[date]) {
            accumulator[date] = {
                date,
                documents: {},
            }
        }

        if (!accumulator[date].documents[currentValue.docTypesName]) {
            accumulator[date].documents[currentValue.docTypesName] = {
                date: currentValue.date,
                docId: currentValue.docId,
                docTypesName: currentValue.docTypesName,
                products: [],
            }
        }

        accumulator[date].documents[currentValue.docTypesName].products.push({
            name: currentValue.name,
            price: currentValue.price,
            image: currentValue.image,
            qunatity: currentValue.quantity,
        })

        return accumulator;
    }, {})
    console.log('result', result);

    return Object.values(result).map((currentValue: IElement) => {

        console.log('currentValue', currentValue);

        if (currentValue.documents) {
            currentValue.documents = Object.values(currentValue.documents);
        }

        return currentValue;
    });
}

console.log(f(orders));

Please tell me how to describe the typing more correctly and
how to correctly type this moment, respectively, get rid of any?
const result: IElement = orders.reduce((accumulator: any, currentValue: IOrder) => {

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Aetae, 2021-05-02
@dc65k

interface IOrder {
    date: string,
    docTypesName: string,
    docId: number,
    image: string,
    name: string,
    price: number,
    quantity: number,
    removed: number,
}

interface  IProduct {
    image: string,
    name: string,
    price: number,
    quantity: number,
}

interface IDocument {
    date: string,
    docId: number,
    docTypesName: string,
    products: IProduct[],
}

interface IElement {
    date: string,
    documents: IDocument[]
}

interface IElementMap {
    date: string,
    documents: Record<string, IDocument>
}

type IResultMap = Record<string, IElementMap>;

function f(orders: IOrder[]): IElement[] {
    const result = orders.reduce((accumulator, currentValue) => {
        const date = currentValue.date.split(' ')[0];

        if (!accumulator[date]) {
            accumulator[date] = {
                date,
                documents: {},
            }
        }

        if (!accumulator[date].documents[currentValue.docTypesName]) {
            accumulator[date].documents[currentValue.docTypesName] = {
                date: currentValue.date,
                docId: currentValue.docId,
                docTypesName: currentValue.docTypesName,
                products: [],
            }
        }

        accumulator[date].documents[currentValue.docTypesName].products.push({
            name: currentValue.name,
            price: currentValue.price,
            image: currentValue.image,
            quantity: currentValue.quantity,
        })

        return accumulator;
    }, {} as IResultMap)

    console.log('result', result);

    return Object.values(result).map(currentValue => {
        console.log('currentValue', currentValue);

        return {
            ...currentValue,
            documents: Object.values(currentValue.documents)
        }
    });
}

console.log(f(orders));

A
Alexey Yarkov, 2021-05-02
@yarkov

}, {} as НужныйТип)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question