ex) Array, String, Map, Set - ์ดํฐ๋ ์ด์ ํ๋กํ ์ฝ์ ๋ฐ๋ฅด๋ ๊ธฐ๋ณธ ์๋ฃ๊ตฌ์กฐ
for..of, spread ์ฐ์ฐ์์์ ์ฌ์ฉํ ์ ์์
js
const arr1 = ["ํ๋", "์ฐ", "๋ฐ๋ค"];
for (const item of arr1.entries()) {
console.log("item.entries()๋? : ", item);
}
console.log();
// for (๋ณ์ (๋ฐฐ์ด์ ์์ดํ
) of ๋ฐฐ์ด) {์คํ๋ฌธ...}
// Array.values() (๊ธฐ๋ณธ๊ฐ. ์๋ต๊ฐ๋ฅ)
// Array.keys() - index ๋์ด
// Array.entries() - index, value ๋์ด
// ์ค๋ธ์ ํธ๋ก for of ์ฌ์ฉ
/*
const obj = { a: 1, b: 2, c: 3 };
for (const item of obj) {
console.log("obj์ ์์ดํ
: ", item);
}
*/
// TypeError : obj is not iterable - ์ค๋ธ์ ํธ๋ ์ดํฐ๋ฌ๋ธ์ด ์๋
// ์ค๋ธ์ ํธ๋ for in ์ฌ์ฉ ๊ฐ๋ฅ
const obj = { a: 1, b: 2, c: 3 };
for (const item in obj) {
console.log("obj์ ์์ดํ
: ", item); // key๊ฐ ์ถ๋ ฅ
}
console.log();
// const arr1 = ["ํ๋", "์ฐ", "๋ฐ๋ค"];
const iter = arr1.values(); // ์ดํฐ๋ ์ดํฐ๋ฅผ ๋ฐํํ๋ ํจ์
console.log("์ฒซ๋ฒ์งธ next : ", iter.next());
// ๊ฒฐ๊ณผ ๊ฐ์ฒด ๋ฆฌํด. { value: 'ํ๋', done: false }
// ์ฒซ ๋ฒ์งธ value: ํ๋, done: ๋๋ฌ๋์ง ์๋์ง ์ฌ๋ถ
console.log("๋๋ฒ์งธ next : ", iter.next());
console.log("์ธ๋ฒ์งธ next์ value : ", iter.next().value);
console.log("๋ค๋ฒ์งธ next : ", iter.next());
console.log("๋ค์ฏ๋ฒ์งธ next์ done : ", iter.next().done);
console.log();
// while ์ด์ฉ
const iter2 = arr1.values();
console.log("iter2 : ", iter2);
while (true) {
const item = iter2.next();
if (item.done) break;
console.log("while๋ฌธ : ", item.value);
}
๋ชจ๋ iterable(๋ฐ๋ณต)์ spread(์ ๊ฐ)๋ ์ ์์
function (...iterable) / [...iterable] / { ...object }
js
// 3๊ฐ์ ์ธ์๋ฅผ ์ ๋ถ ํฉ์ณ์ฃผ๋ ํจ์
function add(x, y, z) {
return x + y + z;
}
const num = [1, 2, 3];
// console.log(add(num[0], num[1], num[2])); // ๋ฒ๊ฑฐ๋ก์
console.log(add(...num)); // spread ์ฐ์ฐ์
// Rest parameters - ํจ์์์ ์ธ์๋ก ๋ฐ์์ฌ ๋ ๊ฐฏ์๊ฐ ์ ํด์ ธ ์์ง ์์
function sum(first, second, ...nums) {
// ์์ ๋๊ฐ๋ ์๋๋ฐ ๋ค์๋ ์ฌ์ฉ์ ๋ง๋๋ก ํ๋ผ๋ ๋ป
console.log("nums๋? : ", nums);
}
sum(1, 2, "ใ
ใ
", 8, 9, 63);
console.log();
// concat์ ์ด์ฉํ์ฌ ๋ฐฐ์ด ํฉ์น๊ธฐ
const fruits1 = ["๐ฅ, ๐ฅฅ"];
const fruits2 = ["๐, ๐"];
let arr = fruits1.concat(fruits2);
console.log(arr); // [ '๐ฅ, ๐ฅฅ', '๐, ๐' ]
// concat์ ์ฌ์ฉํ์ง ์๊ณ ๋ฐฐ์ด ํฉ์น๊ธฐ
arr = [...fruits1, ...fruits2];
console.log(arr); // [ '๐ฅ, ๐ฅฅ', '๐, ๐' ]
arr = [...fruits1, "๐", ...fruits2];
console.log(arr); // [ '๐ฅ, ๐ฅฅ', '๐', '๐, ๐' ] // ๋ฐฐ์ด ์ค๊ฐ์ ์ถ๊ฐ ๊ฐ๋ฅ
console.log();
// Object
const student1 = { name: "sunny", age: 20, home: { address: "seoul" } };
// const student1_1 = { name: "sunny", age: 20, home: { address: "seoul" }, major: "business" };
const student1_1 = { ...student1, major: "business" };
console.log("student1 : ", student1);
console.log("student1_1 : ", student1_1);
js
const fruits = ["๐ฅ", "๐ฅฅ", "๐", "๐", "๐", "๐"];
console.log(fruits[2]); // ์ธ๋ฑ์ค ๋ฒํธ ๋ง๊ณ ์ด๋ฆ์ ๋ถ์ฌ์ ์ถ๋ ฅํ๊ณ ์ถ์ (์๋ฏธ ์๋ ์ด๋ฆ์ผ๋ก)
const [kiwi, coco, grape, ...others] = fruits;
// ๋ฐฐ์ด์ ๊ตฌ์กฐ๊ฐ ๋ถํด๋์ด์ ์์๋๋ก ํ ๋น๋จ. 4๋ฒ์งธ ๋ถํฐ๋ rest parameter๋ก
console.log(coco); // ๋ถํด ํ์๋ ์ธ๋ฑ์ค๋ก ์ ๊ทผํ ํ์x, ๋ฐฐ์ด ์ด๋ฆ ํ์x
console.log("others : ", others);
const point = [1, 2];
const [x, y = 15, z = 1004] = point; // point ๋ฐฐ์ด์ ๊ตฌ์กฐ๋ถํดํ ๋น
// z = 1004๋ ๋์ค์ ์ถ๊ฐ(์ด๊ธฐ๊ฐ ์ค์ ํ ์ถ๊ฐ)
// ์ด๋ฏธ ์์ point ๋ฐฐ์ด์์ ์ง์ ํด์ค ๊ฐ์ด ์๊ธฐ ๋๋ฌธ์ y = 15๋ก ๋ณ๊ฒฝ๋์ง ์์
console.log("x : ", x);
console.log("y : ", y);
console.log("z : ", z);
console.log();
// ํจ์์์ ์ฌ์ฉ
function createEmoji() {
return ["apple", "๐"];
}
console.log(createEmoji()); // ์๋ฏธ์๋ ์ด๋ฆ์ผ๋ก ํ๋์ฉ ์ฐ๊ณ ์ถ์
const [title, emoji] = createEmoji(); // ํจ์๋ฅผ ์ง์ ๊ตฌ์กฐ๋ถํดํ ๋น ๊ฐ๋ฅ
console.log("title : ", title);
console.log("emoji : ", emoji);
console.log();
// ์ค๋ธ์ ํธ์์ ์ฌ์ฉ
const student1 = { name: "sunny", age: 20, major: "business" };
// function display(person) {
// console.log("name? : ", person.name);
// console.log("age? : ", person.age);
// console.log("major? : ", person.major);
// }
function display({ name, age, major }) {
// ์ฒ์๋ถํฐ ๊ตฌ์กฐ๋ฅผ ๋ถํดํด์ ๋ฐ์์ด
console.log("name? : ", name);
console.log("age? : ", age);
console.log("major? : ", major);
}
display(student1);
console.log();
// ๊ทธ๋ฅ ๋ณ์ ํ ๋น
const { name, age, major: job, pet = "๊ฐ์์ง" } = student1;
console.log("name? : ", name);
console.log("age? : ", age);
// console.log("major? : ", major); // major: job (์
๋ฐ์ดํธ)
console.log(job);
console.log(pet); // pet = "๊ฐ์์ง" (์ถ๊ฐ)