๋ด๊ฐ ์ง ์ฝ๋์ ๋ฆฌ์๋์ด ์ง ์ฝ๋์ ์ฐจ์ด์ ์ ๋ํด์
1.์ฝ๋์ ๋ช ํ์ฑ - ๋ณ์๋ช ์ ๋๊ตฐ๊ฐ ๋ด ์ฝ๋๋ฅผ ๋ณด๊ณ ์ดํดํ ์ ์๊ฒ๋ ์งฐ๋๊ฐ? โ ์ฝ๋๊ฐ ๊ธธ์ด์ง๋ค๋ณด๋ฉด ํผ์๋ง ์ดํดํ ์ ์๋ ์ฝ๋๋ช ์ ์ด๋ค๋ ์ง ๋๋ง ์ดํดํ ์ ์๋ ๋ก์ง์ ์ฌ์ฉํด์ ์ฝ๋๋ฅผ ์ง๊ฒ๋๋ค. ๋๊ตฐ๊ฐ์ ํ์ ํ์ฌ ์์ ํ๋ค๋ ๊ฒ์ ๋ช ์ฌํ๊ณ ์์ ํ์. 2.์ค๋ณต์ ์ค์ผ ์ ์๋ ์ง ๊ณ ๋ฏผํด๋ณด์๋๊ฐ? โ ๋ด๊ฐ ์ง ์๋์ ์ฝ๋๋ฅผ ๋ณด๋ฉด ์ค๋ณต๋๋ ๋ถ๋ถ๋ค์ด ๋๋ฌด๋ง๋ค. ์ด๋ ๊ฒ ์ค๋ณต ๋๋ ๋ถ๋ถ๋ค์ด ๋ง๋ค๋ ๊ฑด ์ค๋ณต์ ์ค ์ผ ์ ์๋ ์ฌ์ง๊ฐ ์๋ค๋ ๊ฒ์ ์๋ฏธํ๋ค.
async createLabel(@Args() { labels }: CreateProductInput): Promise<String> {
// input์ ์
๋ ฅ๋ labelName, ๋ผ๋ฒจ isVisible, ์๋ก ์
๋ ฅ๋ labelName๋ง์ ๊ด๋ฆฌํ๊ธฐ ์ํ ๋น ๋ฐฐ์ด
const labelName = [];
const isVisible = [];
for (const inputLabel in labels) {
// ์
๋ ฅ๋ ๋ผ๋ฒจ์ ๋ฐ๋ณต๋ฌธ์ผ๋ก ๋๋ฉด์ ๋ฐฐ์ด labelName๊ณผ isVisible์ ๋ฐ๋ก ๋ฃ์ด์ค๋ค.
const inputLabelValue = Object.values(labels[inputLabel]);
isVisible.push(inputLabelValue[0]);
labelName.push(inputLabelValue[1]);
}
// ์
๋ ฅ๋ฐ์ labelName๊ณผ isVisible ์ฌ๋ถ๋ฅผ ๋ด์ ๋ฐฐ์ด๋ค
//console.log(labelName, isVisible)
// 1. ์ด๋ฏธ ์กด์ฌํ๋ ๋ผ๋ฒจ ์ฌ๋ถ ํ์ธ
const existedLabel = await this.prisma.label.findMany({
where: {
name: {
in: labelName
},
},
select: {
name: true,
id: true,
},
});
const existedLabelWithId = existedLabel.map((item) => {
return { labelId: item.id, labelName: item.name, isVisible: true };
});
// 2. ์๋ก ๋ฑ๋ก๋ ๋ผ๋ฒจ์ Id์ isVisible์ ๋งคํ
const mappedExistedLabel = existedLabelWithId.map((e, i) => {
let temp = labels.find((element) => element.labelName === e.labelName);
if (temp.labelName) {
e.isVisible = temp.isVisible;
}
delete e.labelName;
return e;
});
console.log(mappedExistedLabel);
// labelId์ isVisible๋ก ๋งคํ๋ ํ์กดํ๋ ๋ผ๋ฒจ
// 3. ์๋ก์ด ์
๋ ฅ๋ ๋ผ๋ฒจ์ด ์์ ๊ฒฝ์ฐ
if (existedLabel.length !== 0) {
// 3_1. ์๋ก ๋ฐ๊ฒฌ๋ ๋ผ๋ฒจ๋ง filter๋ก ๊ฑธ๋ฌ ์ ๋ฐฐ์ด์ ๋ด์์ค๋ค.
// ์
๋ ฅ๋ labels๊ณผ existedLabel, ํ์กดํ๋ ๋ผ๋ฒจ์ด ์ผ์นํ์ง ์๋ ๊ฒ๋ง ํํฐ๋ก ๊ฑธ๋ฌ์ ๋ฐฐ์ด์ ๋ด์์ค๋ค.
const foundNewLabel = labels.filter(
(inputLabel) =>
!existedLabel.some(
(inputExistedLabel) => inputLabel.labelName === inputExistedLabel.name,
),
);
// 3_2. ์๋ก ๋ฐ๊ฒฌ๋ ๋ผ๋ฒจ์ prisma๋ก ์์ฑํ๊ธฐ ์ํด labelName => name์ผ๋ก ๋ณ๊ฒฝ
const res = foundNewLabel.map((item) => {
return { name: item.labelName };
});
// 3_3. ์๋ก์ด ๋ผ๋ฒจ๋ค์ createMany๋ฅผ ํตํด label๋ก ๋ฑ๋ก
const newLabel = await this.prisma.label.createMany({
data: res,
});
const newLabelName = [];
const newLabelIsVisible = [];
// 3_4. ์๋ก ๋ฐ๊ฒฌ๋ ๋ผ๋ฒจ์ prisma๋ก ์์ฑํ๊ธฐ ์ํด labelName => name์ผ๋ก ๋ณ๊ฒฝ
const result = foundNewLabel.map((item) => {
return { name: item.labelName, isVisible: item.isVisible };
});
// 3_5. ์๋ก ์
๋ ฅ๋ newLabelName๊ณผ newLabelIsVisible์ ๊ด๋ฆฌํ๊ธฐ ์ํ ๋น ๋ฐฐ์ด
for (const inputLabel in result) {
// ์๋ก์ด ๋ผ๋ฒจ๋ค์ ์กฐํํ๊ธฐ ์ํด์ ๋ฐ๋ณต๋ฌธ์ ๋๋ฉด์ ์๋ก ์
๋ ฅ๋ labelName๊ณผ isVisible์ ๋ฐฐ์ด ์์ ๋ฃ๋๋ค.
const inputLabelValue = Object.values(result[inputLabel]);
newLabelName.push(inputLabelValue[0]);
newLabelIsVisible.push(inputLabelValue[1]);
}
// 4. ์๋ก ๋ฑ๋ก๋ ๋ผ๋ฒจ์ ์กฐํํ๋ค.
const registeredNewLabel = await this.prisma.label.findMany({
where: {
name: {
in: newLabelName,
},
},
select: {
name: true,
id: true,
},
});
// 5. ์๋ก ๋ฑ๋ก๋ ๋ผ๋ฒจ์ Id๋ฅผ labelId๋ก ๋ณ๊ฒฝ isVisible์ ์ถ๊ฐ ํ๊ธฐ ์ํด m
const registeredNewLabelWithId = registeredNewLabel.map((item) => {
return { labelId: item.id, labelName: item.name, isVisible: true };
});
// 6. ์๋ก ๋ฑ๋ก๋ ๋ผ๋ฒจ์ Id์ isVisible์ ๋งคํ
const mappedNewLabel = registeredNewLabelWithId.map((e) => {
let temp = foundNewLabel.find((element) => element.labelName === e.labelName);
if (temp.labelName) {
e.isVisible = temp.isVisible;
}
delete e.labelName;
return e;
});
console.log(mappedNewLabel);
// labelId์ isVisible๋ก ๋งคํ๋ ์๋ก ๋ฑ๋ก๋ ๋ผ๋ฒจ
//ํ์กดํ๋ ๋ผ๋ฒจ๊ณผ ์๋ก ๋ฑ๋ก๋ ๋ผ๋ฒจ์ ํฉ์ณ์ ๋ฐฐ์ด์ ๋ด๋๋ค.
const res= mappedExistedLabel.concat(mappedNewLabel)
// ์ดํ has_labelํ
์ด๋ธ์ reviewTarget์ ์์ฑํ ๋ ํ ๋ฒ์ ์์ฑํ์ฌ has_label ํ
์ด๋ธ์ ๋ฑ๋กํ๋ค.
return res;
}
ERROR๋ฅผ ํด๊ฒฐํ ๐ Set map๊ณผ set์ ๋ํด ์ฐธ๊ณ ํ๋ฉด ์ข์ ๊ธ : https://medium.com/@hongkevin/js-5-es6-map-set-2a9ebf40f96b
Set
ย ๊ฐ์ฒด๋ ์๋ฃํ์ ๊ด๊ณ ์์ดย ์์ ๊ฐ๊ณผย ๊ฐ์ฒด ์ฐธ์กฐย ๋ชจ๋ ์ ์ผํ ๊ฐ์ ์ ์ฅํ ์ ์์ต๋๋ค.// Array ๊ฐ์ฒด์์ ๊ด๊ณ
var myArray = ['value1', 'value2', 'value3'];
// Array๋ฅผ Set์ผ๋ก ๋ณํํ๊ธฐ ์ํด์๋ ์ ๊ท Set ์์ฑ์ ์ฌ์ฉ
var mySet = new Set(myArray);
mySet.has('value1'); // true ๋ฐํ
// set์ Array๋ก ๋ณํํ๊ธฐ ์ํด ์ ๊ฐ ์ฐ์ฐ์ ์ฌ์ฉํจ.
console.log([...mySet]); // myArray์ ์ ํํ ๊ฐ์ ๋ฐฐ์ด์ ๋ณด์ฌ์ค
has()
ย ๋ฉ์๋๋ย Set
ย ๊ฐ์ฒด์ ์ฃผ์ด์ง ์์๊ฐ ์กด์ฌํ๋์ง ์ฌ๋ถ๋ฅผ ํ๋ณํด ๋ฐํํฉ๋๋ค.const set1 = new Set([1, 2, 3, 4, 5]);
console.log(set1.has(1));
// expected output: true
console.log(set1.has(5));
// expected output: true
console.log(set1.has(6));
// expected output: false
Set
์ด๋ผ๋ ์๋ฃ ๊ตฌ์กฐ๋ฅผ ์ฐ๋ ์ด์ ์ ๋ํด์Q. Set
์ ํ์ฉํ์ฌ ์ค๋ณต์ ์ ๊ฑฐํ๊ธฐ ์ํด ์ฐ์ด๋๊ฑด์ง?
A: Set ์๋ฃ๊ตฌ์กฐ๋ ์ค๋ณต์ ์ ๊ฑฐํ๋๋ฐ ์์ด ์ค๋ฌด์์ ๋ง์ด ์ฐ์ธ๋ค.
EX> [{ isVisible: true, labelid: '๋์ดํค์กฐ๋' }, { isVisible: false, label: '๋์ดํค์กฐ๋' }]
โ Set
์ ํ์ฉํ๋ค๋ฉด ์ด๋ฌํ ํํ์ ์ค๋ณต์ ์ ๊ฑฐํ ์ ์๋ค.
Q. Set์ด ์ ๊ณตํ๋ has
์ Array์์ ์ธ ์ ์๋ includes
์ ์ฑ๋ฅ ์ฐจ์ด๊ฐ ์๋์ง?
has
๋ฉ์๋ ๋๋ฌธ์ธ์ง? has๋ includes
์ ๋ชฉ์ ์์ฒด๋ ํฌ๊ฒ ์ฐจ์ด๊ฐ ์์ด ๋ณด์๋ค.