์ƒˆ์†Œ์‹

Languages/Java Script

Rest Parameter | Map | Maplterator | Filtering & Set | Set

  • -
๋ฐ˜์‘ํ˜•

๋ชจ๋“  ํฌ์ŠคํŒ…์€ ์ธํ”„๋Ÿฐ ECMAScript6: ๋น ๋ฅด๊ฒŒ ๋ฐฐ์šฐ๋Š” ES6 ์Šคํ‚ฌ๊ณผ ๋น„๋™๊ธฐ ํ”„๋กœ๊ทธ๋žจ ์˜ ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ์œผ๋ฉฐ

์ถœ์ฒ˜๋Š” ์•„๋ž˜์˜ ์ฃผ์†Œ๋กœ "๊น€ํ˜•ํƒœ"๋‹˜์˜ ์ž๋ฃŒ์ž…๋‹ˆ๋‹ค. 

์ƒ์—…์ ์ธ ์˜๋„๊ฐ€ ์•„๋‹Œ ๊ณต๋ถ€ํ•œ ๊ฒƒ์„ ์ •๋ฆฌํ•ด๋†“๋Š” ๋ชฉ์ ์œผ๋กœ ํฌ์ŠคํŒ… ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

www.inflearn.com/course/es6-ecmascript-6

 

ECMAScript 6: ๋น ๋ฅด๊ฒŒ ๋ฐฐ์šฐ๋Š” ES6 ์Šคํ‚ฌ๊ณผ ๋น„๋™๊ธฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ์ธํ”„๋Ÿฐ

๋น„๋™๊ธฐ ํ™˜๊ฒฝ์—์„œ ์‰ฝ๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ES6์˜ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์— ๋Œ€ํ•ด ๋ฐฐ์šฐ๊ณ  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์Šคํ‚ฌ์„ ํ–ฅ์ƒ์‹œ์ผœ ๋ณด์„ธ์š”. ์ดˆ๊ธ‰ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด ES6 ์˜จ๋ผ์ธ ๊ฐ•์˜ abcdefghijk

www.inflearn.com


1. Rest Parameter

# ๋‚˜๋จธ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜(rest parameter)

ES6์—์„œ๋Š” ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์ง€์›๋ฒ”์œ„๋ฅผ ํฌ๊ฒŒ ํ™•๋Œ€ํ•˜๊ธฐ ์œ„ํ•ด ๊ฐ€๋ณ€ ์ธ์ž์— ๋Œ€ํ•œ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ–ˆ๋‹ค.

๋งˆ์นจํ‘œ ์„ธ๊ฐœ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋‚˜๋จธ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜ "..." ํ‚ค์›Œ๋“œ๊ฐ€ ์ถ”๊ฐ€๋œ ๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ์„ "Rest Parameter"๋ผ๊ณ  ํ•œ๋‹ค. ์ด ํ‚ค์›Œ๋“œ๋Š” ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•  ๋•Œ ๋งค๊ฐœ๋ณ€์ˆ˜์— ์‚ฌ์šฉ๋œ๋‹ค.

function myFunction(a,b,c,d, ...rest){
    console.log(a,b);
    console.log(rest);
    console.log(rest.length);
}
myFunction(1,2,3,4,5,6,7,8);

์œ„ ์˜ˆ์‹œ์˜ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

 

myFunctionํ•จ์ˆ˜๋Š” ๋‹ค์„ฏ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜์— rest parameter๋ฅผ ์ •์˜ํ•˜์˜€๋‹ค.ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ ์‹œ, ๋„˜๊ฒจ์งˆ ์ธ์ž๋“ค ์ค‘ 5๋ฒˆ์งธ ๋ถ€ํ„ฐ๋Š” ๋ฐฐ์—ด๋กœ ๋ฐ›๋Š”๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

๋”ฐ๋ผ์„œ 3๋ฒˆ ๋ผ์ธ์—์„œ 5,6,7,8์„ ๊ฐ€์ง„ ๋ฐฐ์—ด์ด ์ถœ๋ ฅ๋˜์—ˆ๋‹ค. ๋ฐฐ์—ด์ธ ์ ์„ ์ด์šฉํ•ด length์†์„ฑ์„ ์ด์šฉํ•ด ๊ธธ์ด๋ฅผ ๊ตฌํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

# ์ „๊ฐœ ์—ฐ์‚ฐ์ž(spread operator) **๋งค์šฐ ์ค‘์š”

ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ด ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น ๋ฐฐ์—ด์„ ํŽผ์ณ์„œ ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ธ์ž๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

์˜ˆ์‹œ๋ฅผ ์‚ดํŽด๋ณด์ž!

function rest(a,b,c,...rest){
    console.log(b,c);
    
    for(let i of rest){
    	console.log(i);
    }
    
}

var spread = [2,3];

rest(1, ...spread, 4, ...[5,6];

๋งจ ๋์ค„ ๋ผ์ธ์„ ๋ณด๋ฉด ...ํ‚ค์›Œ๋“œ ์ฆ‰ ์Šคํ”„๋ ˆ๋“œ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ๋ฐฐ์—ด์ด ํŽผ์ณ์กŒ๊ณ , 1,2,3 ์„ธ๊ฐœ์˜ ์ธ์ž๋Š” ์ œ์ผ ์ฒซ ์ค„ a,b,c ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๊ฐ๊ฐ ๋„˜๊ฒจ์กŒ๋‹ค.

 

์ด๋ ‡๊ฒŒ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜์ง€ ์•Š๊ณ ๋„ ๋ฐฐ์—ด์— ๋ฐ”๋กœ ์œ„์™€ ๊ฐ™์ด "..."ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์œ„ ํ•จ์ˆ˜์—์„œ restํŒŒ๋ผ๋ฏธํ„ฐ๋Š” 4,5,6์„ ์ธ์ž๋กœ ๋ฐ›์•˜๊ณ , ์ƒ์„ฑ๋œ rest iterator๋Š” for-of๊ตฌ๋ฌธ์„ ํ†ตํ•ด ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๊ฒŒ๋œ ๊ฒƒ์ด๋‹ค.

 

์ด์ฒ˜๋Ÿผ ๋ฐ์ดํ„ฐ๊ฐ€ ๋™์ ์œผ๋กœ ๋ฐ”๋€Œ๋Š” ๊ฒฝ์šฐ ๊ฐ€๋ณ€์ธ์ž๋ฅผ ๋‹ค๋ฃฐ ๋•Œ ๋งค์šฐ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค.

 

 

์•„๋ž˜ ๋ธ”๋กœ๊ทธ ๊ธ€์„ ๋ณด์‹œ๋ฉด ์กฐ๊ธˆ ๋” ์ž์„ธํžˆ ์ดํ•ด๊ฐ€ ๋  ๊ฒƒ ๊ฐ™๋„ค์š”!

paperblock.tistory.com/62

 

[ES6] Spread Operator (์Šคํ”„๋ ˆ๋“œ ์—ฐ์‚ฐ์ž)

ES6์—์„œ๋Š” '...'์™€ ๊ฐ™์ด ๋‹ค์†Œ ํŠน์ดํ•œ ํ˜•ํƒœ์˜ ๋ฌธ๋ฒ•์ด ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์  3๊ฐœ๊ฐ€ ์—ฐ๋‹ฌ์•„ ๋ถ™์–ด์žˆ๋Š” ์ด ํ‘œ์‹œ๋Š” Spread Opertor(์Šคํ”„๋ ˆ๋“œ ์˜คํผ๋ ˆ์ดํ„ฐ, ์Šคํ”„๋ ˆ๋“œ ์—ฐ์‚ฐ์ž, ์ „๊ฐœ ๊ตฌ๋ฌธ, ํŽผ์นจ ์—ฐ์‚ฐ์ž...)๋ฅผ ๋‚˜ํƒ€๋‚ด

paperblock.tistory.com

 

# ์ •๋ฆฌํ•˜๊ธฐ

๋‚˜๋จธ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋‘ ๊ฐœ ์ด์ƒ์˜ ์š”์†Œ๋ฅผ ํ•˜๋‚˜์˜ ์š”์†Œ๋กœ ๋ชจ์œผ๋Š” ๊ฒƒ์ด์ง€๋งŒ

์ „๊ฐœ ์—ฐ์‚ฐ์ž๋Š” ๋ฐฐ์—ด์ด๋‚˜ ๊ฐ์ฒด๋ฅผ ํ™•์žฅํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋‚˜๋จธ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜์™€ ๋Œ€์กฐ๋œ๋‹ค.

 

 

2. Map

# Map

์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ธ "Map๊ณผ Set"์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.

Map์€ key์™€ value๊ฐ€ ํ•œ ์Œ์ธ ์ปฌ๋ ‰์…˜์ด๋‹ค.

javascript์—์„œ๋Š” ๊ฐ์ฒด์™€ ๋ฐฐ์—ด์ด ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์žˆ์ง€๋งŒ, ํŠน์ • ๊ฐ’์ด ์žˆ๋Š” ์ง€ ์ฐพ์•„๋‚ด๋ ค๋ฉด ๋ณ„๋„์˜ ์ฒ˜๋ฆฌ๊ณผ์ •์ด ํ•„์š”ํ•˜์—ฌ ๋ถˆํŽธํ–ˆ๋‹ค.

์œ„ ๋‘ ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด ๊ทธ๋Ÿฌํ•œ ํ•จ์ˆ˜๋“ค์„ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•œ๋‹ค.

 

์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ์‚ดํŽด๋ณด์ž

<script>
const myMap = new Map();
myMap.set("Lang","JavaScript");
</script>

์ด๋ ‡๊ฒŒ newํ‚ค์›Œ๋“œ๋กœ Map()์„ ์ƒ์„ฑํ•˜๊ณ  .set๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ํ‚ค์™€ ๊ฐ’์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋˜ํ•œ, set ๋ฉ”์†Œ๋“œ๋Š” ์—ฐ์†ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ํ• ๋‹น ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ๋ณด์ž.

<script>
const myMap = new Map();
myMap.set("plugin","update").set("refactor",6).set("code",5);
</script>

 

 

 

map์€ ์ดˆ๊ธฐํ™” ํ•˜๋ฉด์„œ ๋ฐฐ์—ด๋กœ ๊ฐ’์„ ํ• ๋‹นํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

<script>
const myData = new Map([
    ["key1","value1"],
    ["key2", "value2"]
]);
const.log(myData.get("key1"));
</script>

 

๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

์ด์ฒ˜๋Ÿผ get๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ key์— ํ•ด๋‹นํ•˜๋Š” value๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

 

 

 

์ด์ „์˜ ํ•™์Šตํ•œ ๋ฐฉ์‹์œผ๋กœ map์— ๋‹ด์„ ํ‚ค์™€ ๊ฐ’์„ ๋ฐฐ์—ด์— ๋‹ด์•„ ์ดํ„ฐ๋ ˆ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž.

 

<script>
const myMap = new Map();
const myMap = [["plugin","update"],["refactor",6],["code",5]];

for(let i of myArray){
    myMap.set(...i);
}
myMap.delete("hello");
for(let [key, value] of myMap){	
    console.log(key + "," + value);
}
</script>

(์ „๊ฐœ ์—ฐ์‚ฐ์ž์˜ ๋Œ€ํ•œ ์„ค๋ช…์€ ๋ฐ”๋กœ ์œ„์ชฝ์— ํฌ์ŠคํŒ… ํ•ด๋‘์—ˆ์Šต๋‹ˆ๋‹ค.)

 

iterator์˜ ๊ฐœ๋…์ด ํ™•์‹คํžˆ ์™€๋‹ฟ์ง€ ์•Š๋Š”๋‹ค๋ฉด ์•„๋ž˜ ์ฃผ์†Œ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ๊ณต๋ถ€ํ•ด๋ณด์ž!

developer.mozilla.org/ko/docs/Web/JavaScript/Guide/Iterators_and_Generators

 

๋ฐ˜๋ณต๊ธฐ ๋ฐ ์ƒ์„ฑ๊ธฐ - JavaScript | MDN

์ปฌ๋ ‰์…˜ ๋‚ด ๊ฐ ํ•ญ๋ชฉ ์ฒ˜๋ฆฌ๋Š” ๋งค์šฐ ํ”ํ•œ ์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค. JavaScript๋Š” ๊ฐ„๋‹จํ•œ for ๋ฃจํ”„์—์„œ map() ๋ฐ filter()์— ์ด๋ฅด๊ธฐ๊นŒ์ง€, ์ปฌ๋ ‰์…˜์„ ๋ฐ˜๋ณตํ•˜๋Š” ๋งŽ์€ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ณต๊ธฐ(iterator) ๋ฐ ์ƒ์„ฑ๊ธฐ(generator)๋Š”

developer.mozilla.org

 

์ด ๋ฐฉ์‹์€ for-of๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜๊ฐ€ ์žˆ๊ฒŒ ๋œ๋‹ค.

์ด์ฒ˜๋Ÿผ set๋ฉ”์†Œ๋“œ์™€ ์ธ์ž i์˜ ์ „๊ฐœ ์—ฐ์‚ฐ์ž๋กœ myArray๋ฐฐ์—ด์„ ํ’€์–ด map์— ๋‹ด์„ ์ˆ˜ ์žˆ๋‹ค.

 

map์—์„œ key๋Š” ๋งค์šฐ ๊ณ ์œ ํ•˜๊ณ , ์œ ์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— delete ๋ฉ”์†Œ๋“œ์— key๋ฅผ ๋„ฃ์–ด์„œ ์‚ญ์ œํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

์ด์™€ ๊ฐ™์ด myMap๊ฐ์ฒด๊ฐ€ ๋ฃจํ”„๋ฅผ ๋Œ๋ฉฐ return๋œ ํ˜•์‹์„ let ์„ ์–ธ์ž ์˜ค๋ฅธ์ชฝ์— ๋‚˜์—ดํ•˜์—ฌ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง์ด ์ด๋ฃจ์–ด์กŒ๋‹ค.

 

๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ์ด๋ž€?

lucete1230-cyberpolice.tistory.com/395?category=923624

 

Template Literals | Destructuring | class

๋ชจ๋“  ํฌ์ŠคํŒ…์€ ์ธํ”„๋Ÿฐ ECMAScript6: ๋น ๋ฅด๊ฒŒ ๋ฐฐ์šฐ๋Š” ES6 ์Šคํ‚ฌ๊ณผ ๋น„๋™๊ธฐ ํ”„๋กœ๊ทธ๋žจ ์˜ ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ์œผ๋ฉฐ ์ถœ์ฒ˜๋Š” ์•„๋ž˜์˜ ์ฃผ์†Œ๋กœ "๊น€ํ˜•ํƒœ"๋‹˜์˜ ์ž๋ฃŒ์ž…๋‹ˆ๋‹ค. ์ƒ์—…์ ์ธ ์˜๋„๊ฐ€ ์•„๋‹Œ ๊ณต๋ถ€ํ•œ ๊ฒƒ์„ ์ •๋ฆฌํ•ด

lucete1230-cyberpolice.tistory.com

 

 

์ด๋ฅผ ํ†ตํ•ด ํ‚ค์™€ ๊ฐ’์ด ๋ณ„๋„์˜ ๋ณ€์ˆ˜๋กœ ๋ถ„๋ฆฌ๋˜์—ˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

์ดํ•ด๋ฅผ ๋•๊ธฐ ์œ„ํ•ด ์ฝ”๋“œ๋ฅผ ๊ฑฐ์˜ ๋งค์ค„ ๋งˆ๋‹ค console๋กœ ์ถœ๋ ฅํ•˜์˜€๋‹ค.

1. ์ฒ˜์Œ map์— ๊ฐ’์ด ๋“ค์–ด๊ฐ€๊ธฐ ์ „

2. myArray์— ๊ฐ’์„ ๋„ฃ์€ ํ›„

3. for-of๋ฌธ์„ ํ†ตํ•ด์„œ myArray๊ฐ’์„ myMap์— set ํ•ด์คŒ (๋‹ด์•„์คŒ)

4. delete๋Š” ๊ณ ์œ ํ•œ ํ‚ค๊ฐ’์„ ์ž…๋ ฅํ•˜์—ฌ map์˜ ๊ฐ’์„ ์ง€์šธ ์ˆ˜ ์žˆ์Œ. ํ˜„์žฌ plugin์„ ์ง€์›Œ์ค€ ์ƒํƒœ

 

 

3. MapIterator

# MapIterator ํ™œ์šฉ

for-of ๊ตฌ๋ฌธ์„ ์ด์šฉํ•ด map์„ ์ˆœํ™˜ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ์ด์ฒ˜๋Ÿผ map์€ for-of ์ด ์™ธ์—๋„ ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•ด์ค€๋‹ค.

๋Œ€ํ‘œ์ ์œผ๋กœ for-of, entries(), next(), keys(), values() ๋“ฑ๋“ฑ...

 

์•„๋ž˜์˜ ์˜ˆ์‹œ๋กœ ํ•œ๋ฒˆ์— ์‚ดํŽด๋ณด์ž.

<script>
const myMap = new Map();
const myArray = [["name","haeun"],["age",23],["hobby","piano"]];
for(let i of myArray){
    myMap.set(...i);
}
const myKeys = myMap.keys(),
	myValue = myMap.values(),
    myIter = myMap.entries();
console.log(myKeys);
console.log(myValue);
console.log(myIter);

</script>

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

keys ๋ฉ”์„œ๋“œ์™€ valuse๋ฉ”์„œ๋“œ๋Š” ๊ฐ๊ฐ key์™€ value๋ฅผ ๊ฐ’์œผ๋กœ ๊ฐ€์ง€๋Š” Map Iterator๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ ,

entries()๋Š” [key, value]๋ฅผ ๊ฐ€์ง€๋Š” MapIterator๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

๋ฐ˜๋ณต๋ฌธ์„ for-of๊ฐ€ ์•„๋‹Œ ์ˆ˜๋™์œผ๋กœ ํ™•์ธํ•ด๋ณด์ž.(for-of์˜ ์—ญํ• ์€ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋Š” ๊ฒƒ. ์ถœ๋ ฅ ์‹œ next()๋กœ ๋‹ค์Œ ๋ฐฐ์—ด ๊ฐ’์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ฃผ๋ ค๋Š” ๊ฒƒ์ž„)

iterator๋Š” next๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

<script>
const myMap = new Map();
const myArray = [["name","haeun"],["age",23],["hobby","piano"]];
for(let i of myArray){
    myMap.set(...i);
}
const myKeys = myMap.keys(),
	myValue = myMap.values(),
    myIter = myMap.entries();
    
console.log(myIter.next());
console.log(myIter.next());
console.log(myIter.next());
console.log(myIter.next());


</script>

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

์ด๋ ‡๊ฒŒ next๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ return๊ฐ’์˜ done์ด true (๋์„ ์˜๋ฏธ)๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ์ˆœํ™˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

 

4. Filtering & Set

# map ํ•„ํ„ฐ๋ง ์‹ค์Šต

map์„ ํŠน์ • ์กฐ๊ฑด์œผ๋กœ ํ•„ํ„ฐ๋ง ํ•˜์—ฌ ๋‹ค๋ฅธ map์— ๋‹ด์•„๋ณด์ž.

<script>
const myMap = new Map();
const myArray = [["name","haeun"],["age",23],["hobby","piano"]];
for(let i of myArray){
    myMap.set(...i);
}
const newMap = new Map (
    [...myMap].filter(([a,b])=>b==23));
console.log(newMap);

</script>

์ƒ๋‹จ์— myMap์—์„œ ์ค‘๊ฐ„์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’๋งŒ ์ƒˆ๋กœ์šด map์— ๋‹ด์€ ๊ฒƒ์ด๋‹ค. (์ด๋ถ€๋ถ„ -> ["age",23])

์ถœ๋ ฅ๊ฒฐ๊ณผ : Map {"age" => 23}

์ด์™€ ๊ฐ™์ด ๋งต์— ์ „๊ฐœ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ’€์–ด์„œ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋ฏ€๋กœ, ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

# Set

javascript์˜ ๋ฐฐ์—ด๊ณผ ์œ ์‚ฌํ•˜์ง€๋งŒ, ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์—์„œ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค. ์ฆ‰, ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š” ์ง‘ํ•ฉ์„ ๋งํ•œ๋‹ค. 

map()๊ณผ ์‚ฌ์šฉ๋ฒ•์ด ๋งค์šฐ ๋น„์Šทํ•˜์ง€๋งŒ, ํฐ ์ฐจ์ด์ ์€ key๊ฐ€ ์—†์ด value๋งŒ ์กด์žฌํ•œ๋‹ค๋Š” ์ ์ด๋‹ค.

 

์˜ˆ์‹œ๋ฅผ ์‚ดํŽด๋ณด์ž.

<script>
var set = new Set();
set.add("ECMAScript").add("6");
console.log(set.has("ECMAScript"));//๊ฐ’์ด ์กด์žฌ -> true
console.log(set.has("haeun")); //์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฐ’ -> false

console.log(set.size);
console.log(...set.values());
</script>

๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

has() : ํ•ด๋‹น ๊ฐ’์˜ ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธ

size() : ๊ฐœ์ˆ˜ ํ™•์ธ

 

set์€ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ๊ฐ’์ด ์žˆ๋‹ค๋ฉด ์ถ”๊ฐ€๋กœ ์‚ฝ์ž…ํ•˜๋ ค ํ•ด๋„ ์•„๋ฌด๋Ÿฐ ๋ณ€ํ™”๊ฐ€ ์—†๋‹ค

 

# ์ •๋ฆฌํ•˜๊ธฐ

 

- Map์€ key์™€ value๊ฐ€ ํ•œ ์Œ.

- Set์€ value๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ์ง‘ํ•ฉ

๋ฐ˜์‘ํ˜•

'Languages > Java Script' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Promise & then | chaining & catch | all  (0) 2021.02.15
Generator & yield | yield* | return  (0) 2021.02.15
Iterator & for-of | Symbol | [Symbol.iterator()]  (0) 2021.02.15
Arrow Function & this | IIFE | Default Parameter  (0) 2021.02.15
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.