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)
Author And Source
이 문제에 관하여(To apply for my dirty code(3)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@kerem119/To-apply-for-my-dirty-code3저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)