To apply for my dirty code(1)

I tried to aggregate some data and make structured shape.
Here is the example of data.

const dummyData = [
    {
        userId:1, 
        upDownAt:"2020312-101030", 
        detail:[{}], 
        result: {
            by: 100,
            successCount: 2,
            failCount: 3,
            invalidCount:0
        }   
    
    }, 
    
    {
        userId:2, 
        upDownAt:"2020312-101020", 
        detail:[{}], 
        result: {
            by: 200,
            successCount: 2,
            failCount: 1,
            invalidCount:2
        }
    },

    {
        userId:1, 
        upDownAt:"2020312-105020", 
        detail:[{}], 
        result: {
            by: 100,
            successCount: 1,
            failCount: 2,
            invalidCount: 2
        }
    },
    {
        userId:1, 
        upDownAt:"2020312-111020", 
        detail:[{}], 
        result: {
            by: 300,
            successCount: 4,
            failCount: 0,
            invalidCount: 1
        }
    },
    {
        userId:2, 
        upDownAt:"2020312-103020", 
        detail:[{}], 
        result: {
            by: 300,
            successCount: 4,
            failCount: 0,
            invalidCount: 1
        }
    }
]

and I made first code for aggregate aggregation.

let personalAggregate = async(data) => {

    let totalNumOfUse = data.length;
    let reward = 0;

    for(let i in data) {

        let userId = data[i].userId;

        // let sameTest = data.filter(function(item){
        //     return item.userId == userId
        // });

        // console.log('sametest',sameTest)
    }

    let sameId = data.filter(function(item){
        return item.userId == "1"
    });
    
    let numOfUse = sameId.length;

    for(let i in sameId) {

        let result = sameId[i].result.by;

        reward += result;
    }

    console.log("ID:")
    console.log("Number of Use", numOfUse);
    console.log("Total:", reward)
    
}


personalAggregate(dummyData);

Look at that code...
Isn't it so terrible?

I didn't use some variables and I could't code well about loop also.

I will fix them in next step.

좋은 웹페이지 즐겨찾기