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