Javascript_ 코드 복습
배열 생성으로 반복문 제거
const sum = Array
.from(new Array(5), (_, k) => k + 5)
.reduce((acc, cur) => acc + cur, 0);
배열 내 같은 요소 제거
const names = ['Lee', 'Kim', 'Park', 'Lee', 'Kim'];
const uniqueNamesWithArrayFrom = Array.from(new Set(names));
const uniqueNamesWithSpread = [...new Set(names)];
구조 분해 할당을 이용한 변수 바꾸기
let a = 5, b = 10;
[a, b] = [b, a];
console.log(a, b);
객체 병합
const person = {
name: 'jj',
familyName: 'jjj',
givenName: 'jjjjj
};
const company = {
name: 'kkk.',
address: 'kkkkk'
};
const kj = { ...person, ...company };
console.log(kj);
&& , ||
const name = participantName || 'Guest';
flag && func();
const makeCompany = (showAddress) => {
return {
name: 'Cobalt. Inc.',
...showAddress && { address: 'Seoul' }
}
};
console.log(makeCompany(false));
console.log(makeCompany(true));