for...in ๐Ÿ‘€ for..of

soo'sยท2023๋…„ 4์›” 6์ผ
1

Curious Coder Chronicles

๋ชฉ๋ก ๋ณด๊ธฐ
1/4

์ฐธ์กฐํ˜• ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ๊ฐ’ ๋ณต์‚ฌ๋ฅผ ๊ณต๋ถ€ํ•˜๋‹ค๊ฐ€ ์–•์€ ๋ณต์‚ฌ๋ฅผ ์œ„ํ•ด for of๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๋ฐฐ์› ๋‹ค.
๊ทธ๋Ÿฐ๋ฐ ๊ทธ ๊ณผ์ •์„ for in์œผ๋กœ ํ•ด๋„ ๋˜๋Š”๊ฑฐ ์•„๋‹Œ๊ฐ€ ์‹ถ์–ด์„œ for of์™€ for in ์ •ํ™•ํ•œ ์ฐจ์ด๊ฐ€ ๊ถ๊ธˆํ•ด์กŒ๋‹ค!

๊ฒฐ๋ก 

๊ฒฐ๋ก ๋ถ€ํ„ฐ ๋งํ•˜์ž๋ฉด for in์€ ๊ฐ์ฒด์—๊ฒŒ ์ตœ์ ํ™” ๋˜์–ด์žˆ๊ณ , for of๋Š” ์ปฌ๋ ‰์…˜(array, string, map, set)์—๊ฒŒ ์ตœ์ ํ™” ๋˜์–ด์žˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์•„๋ž˜์—์„œ ์„ค๋ช…ํ•˜๊ฒ ๋‹ค.

๐Ÿ. for...in

for in์€ mdn ๋ฌธ์„œ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์„ค๋ช…ํ•˜์ž๋ฉด,

for...in๋ฌธ์€ ์ƒ์†๋œ ์—ด๊ฑฐ ๊ฐ€๋Šฅํ•œ ์†์„ฑ๋“ค์„ ํฌํ•จํ•˜์—ฌ ๊ฐ์ฒด์—์„œ ๋ฌธ์ž์—ด๋กœ ํ‚ค๊ฐ€ ์ง€์ •๋œ ๋ชจ๋“  ์—ด๊ฑฐ ๊ฐ€๋Šฅํ•œ ์†์„ฑ์— ๋Œ€ํ•ด ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. (Symbol๋กœ ํ‚ค๊ฐ€ ์ง€์ •๋œ ์†์„ฑ์€ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค.)
... ์ค‘๋žต
์ž„์˜์˜ ์ˆœ์„œ๋กœ ๊ฐ์ฒด์˜ ์†์„ฑ๋“ค์— ๋Œ€ํ•ด ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ ‡๋‹ค . for in์€ ์ž„์˜์˜ ์ˆœ์„œ๋กœ ๊ฐ์ฒด์˜ ์†์„ฑ๋“ค์— ๋Œ€ํ•ด ๋ฐ˜๋ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์— index๋ผ๋Š” ์ค‘์š”ํ•œ ์ •๋ณด๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๋ฐฐ์—ด์—์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋ง์งฑ ๋„๋ฃจ๋ฌต์ด๋ผ๋Š”๊ฑฐ๋‹ค. mdn ๋ฌธ์„œ์—์„œ๋„ for in์„ ๋ฐฐ์—ด์— ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๊ฒƒ์„ ๊ถŒ์žฅํ•˜๊ณ  ์žˆ๋‹ค.

๊ทธ๋ฆฌํ•˜์—ฌ ์ธ๋ฑ์Šค ์ •๋ณด๋ฅผ ๋ฌด์‹œํ•˜๋Š” for in์„ ๋ฐฐ์—ด์— ํ™œ์šฉํ•˜๋ฉด ์•ˆ๋˜๋Š” ๊ฒƒ์€ ์ดํ•ดํ–ˆ์ง€๋งŒ, for of์˜ ๊ฒฝ์šฐ ๊ฐ์ฒด์—์„œ๋„ ์ž˜ ์‚ฌ์šฉํ–ˆ๋˜๊ฑฐ ๊ฐ™์€๋ฐ ๋ญ๊ฐ€ ๋‹ค๋ฅด์ง€! ์‹ถ์—ˆ๋‹ค.

for...of

for of๋Š” mdn ๋ฌธ์„œ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์„ค๋ช…ํ•˜์ž๋ฉด,

for...of ๋ช…๋ น๋ฌธ์€ ๋ฐ˜๋ณต๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด (Array, Map, Set, String, TypedArray, arguments ๊ฐ์ฒด ๋“ฑ์„ ํฌํ•จ)์— ๋Œ€ํ•ด์„œ ๋ฐ˜๋ณตํ•˜๊ณ  ๊ฐ ๊ฐœ๋ณ„ ์†์„ฑ๊ฐ’์— ๋Œ€ํ•ด ์‹คํ–‰๋˜๋Š” ๋ฌธ์ด ์žˆ๋Š” ์‚ฌ์šฉ์ž ์ •์˜ ๋ฐ˜๋ณต ํ›„ํฌ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฃจํ”„๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

์ฒซ ๋ฒˆ์งธ ์˜๋ฌธ : ๊ฐ์ฒด๋„ for...of ์ž˜ ์ผ๋˜๊ฑฐ ๊ฐ™์€๋ฐ?
๊ทธ๋ฆฌํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์งœ๋ณด๋ฉด

let person = {
	name : 'jisu',
   age : 12
};
for (let i of person) {
	console.log(i); // TypeError : person is not literable! 
}

person ๊ฐ์ฒด๋ฅผ for of ๋ฌธ์— ๋ฐ”๋กœ ์‚ฌ์šฉํ•ด๋ณด๋ฉด ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•˜์ง€ ์•Š๋‹ค๊ณ  ๋‚˜์˜จ๋‹ค! ๊ทธ๋ž˜์„œ Object.keys()๋‚˜ Object.entries() ๋“ฑ์„ ์‚ฌ์šฉํ–ˆ๊ตฌ๋‚˜ ์‹ถ์—ˆ๋‹ค. ์Ÿค๋„ค๋Š” ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋‹ˆ๊นŒ ๋ฐ”๋กœ For of๋ฌธ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ๋˜๊ฑฐ์ง€..

let person = {
	name : 'jisu',
   age : 12
};
for (let [key, value] of Object.entries(person)) {
	console.log(i); 
 	// name jisu
  	// age 12
}

๋”ฐ๋ผ์„œ ์œ„์™€ ๊ฐ™์ด ๊ฐ์ฒด์˜ ํ‚ค ๊ฐ’์ด๋‚˜ ๋ฐธ๋ฅ˜, ํ”„๋กœํผํ‹ฐ ์ „์ฒด๋“ฑ์„ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ for of ๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค!


๋‘ ๋ฒˆ์งธ ์˜๋ฌธ : map, set๋„ ๋œ๋‹ค๊ณ ?


let a = new Map();
a.set(1,'one');
a.set(2,'two');
// a = Map(2) {1 => 'one', 2 => 'two'}

for (let i of a) {
    console.log(i);
  // [1, 'one']
  // [2, 'two']
}

map ์ž๋ฃŒ๊ตฌ์กฐ๋กœ for of ๋Œ๋ฆฌ๋ฉด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•จ ใ…‡ใ…‡
๊ทธ๋Ÿผ set์€...

let set = new Set();
set.add(1);
set.add('two');
set.add([3, 'three]);
// set = Set(3) {1, 'two', Array(2)}

for (let i of set) {
	console.log(i, typeof i);
    // 1 'number'
	// two string
	// [3, 'three'] 'object'
}

์ €๋ฒˆ์— ์•ผ๊ตฌ๊ฒŒ์ž„ ๋žœ๋ค ์ˆซ์ž ์ƒ์„ฑํ•  ๋•Œ set์œผ๋กœ ํ•˜๊ณ  (์ค‘๋ณต ์•ˆ๋˜๊ฒŒ ํ•˜๋ ค๊ณ ) ๊ทธ๊ฑฐ ํ™œ์šฉํ•  ๋•Œ, set์—์„œ ๋ฐฐ์—ด๋กœ ๋ฐ”๊พธ๊ณ  ๋‹ค์‹œ ์ŠคํŠธ๋ง์œผ๋กœ ๋ฐ”๊ฟจ๋Š”๋ฐ ๊ทธ๋ƒฅ for of๋ฌธ ๋Œ๋ ค์„œ ๋น„๊ตํ•ด๋„ ๋˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค!!!!!!ใ… ใ… 
์ง€๊ธˆ์ด๋ผ๋„ ์•Œ์•„์„œ ๋‹คํ–‰

verified idiot which is me...

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