To apply for my dirty code(3)

I tried to devide code into some functions and parts

first I made aggregate function.

const aggregate = (data, userId) => {

    let count = 0;
    let reward = 0;
    let failedCount = 0;

    const filteredData = data.filter(item => item.userId === userId);

    for(const i in filteredData) {
        
        const { result } = filteredData[i];
        
        if(!result) {
            continue;
        } else {
            count++;
            reward += result.by;
        }
        
        if(result.failCount > 0) {
            failedCount++;
        }
        
    }

    return { userId, reward, count, failedCount };

}

and I tried to make function to get unique ids

const getUniqueIds = (data) => {
    
    let uniqueIds = [];

    for(const i in data) {

        const { userId } = data[i];

        if(!uniqueIds.includes(userId)) {
            uniqueIds.push(userId);
        }

    }

    return uniqueIds;

}

and I used these functions to Main code

const userInfo = [];

const uniqueIds = getUniqueIds(dummyData);

for(const x in uniqueIds) {
    const userId = uniqueIds[x];
    const result = aggregate(dummyData, userId);
    userInfo.push(result);
}

const failureData = [];

const targets = userInfo.filter(item => item.reward === 0 && item.failedCount >= 10);

const failure = (data, userId) => {
    const amount = 300;
    const reason = 'event';
    return {userId, amount, reason}
}

for(const i in targets) {
    const failId = targets[i].userId;
    const failRes = failure(targets, failId);
    failureData.push(failRes)
}
console.log(failureData)

좋은 웹페이지 즐겨찾기