230406 - iterator(์ˆœํšŒ), spread, destructuring

๋ฐฑ์Šน์—ฐยท2023๋…„ 4์›” 10์ผ
1

๐Ÿšฉ javascript

iterator(์ˆœํšŒ)

๐Ÿ“ ์„ค๋ช…

  • ๋ฐ˜๋ณต, ์ˆœํšŒํ•  ์ˆ˜ ์žˆ์Œ.
  • ์ดํ„ฐ๋ ˆ์ด์…˜ ํ”„๋กœํ† ์ฝœ์„ ๋”ฐ๋ฅด๋Š” ๊ฐ์ฒด(๋ฐ˜๋ณต, ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๋Š” ๊ทœ๊ฒฉ์„ ๋”ฐ๋ฅด๋Š” ๊ฐ์ฒด)

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);
}

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด

๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






spread ์—ฐ์‚ฐ์ž (์ „๊ฐœ๊ตฌ๋ฌธ)

๐Ÿ“ ์„ค๋ช…

  • ๋ชจ๋“  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);

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด

๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






๋ฐฐ์—ด destructuring (๊ตฌ์กฐ๋ถ„ํ•ดํ• ๋‹น)

๐Ÿ“ ์„ค๋ช…

  • ๊ตฌ์กฐ๋ฅผ ํŒŒ๊ดด, ๋ถ„ํ•ด ํ›„ ์žฌ๋ฐฐ์น˜


โœ’ ์ฝ”๋“œ์ž‘์„ฑ

์ž…๋ ฅ

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 = "๊ฐ•์•„์ง€" (์ถ”๊ฐ€)

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด

๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






profile
๊ณต๋ถ€ํ•˜๋Š” ๋ฒจ๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€