๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’๊ณต๋ถ€/JS

[JavaScript] JS์˜ ๋ฐ˜๋ณต๋ฌธ

by rindev 2021. 11. 21.

๊ทธ๋™์•ˆ ๋ฐฐ์šด ๊ฒƒ๋“ค์ด ์žˆ์—ˆ๋‹ค...

๊ทธ๋ƒฅ for๋ฌธ์„ ์จ๋„ ๋˜๋Š” ์ƒํ™ฉ์— ๊ตณ์ด๊ตณ์ด map์„ ์‚ฌ์šฉํ•œ ๋‚˜๋Š” ๋ฐ”๋ณด๋‹ค

๊ทธ๋ž˜์„œ ์˜ˆ์ „์— ์ •๋ฆฌํ•ด๋‘” ๋ฐ˜๋ณต๋ฌธ์„ ๊ฐ€์ ธ์™”๋‹ค...

 

๐Ÿ˜Ž ๋‘์ค„ ์š”์•ฝ

๊ฐ์ฒด ์ˆœํšŒ ํ•  ๋•Œ → Object.keys, Object.values, Object.entries

๋ฐฐ์—ด ์ˆœํšŒ ํ•  ๋•Œ → forEach, map, filter, reduce ๋“ฑ๋“ฑ

 

โœ” for

for (let i = 0; i < 10; i++) {
	console.log(i);
}
  • ๋„ˆ๋„์•Œ๊ณ  ๋‚˜๋„์•Œ๊ณ  ๋ชจ๋‘๊ฐ€ ์•„๋Š” ๊ทธ for๋ฌธ

 

โœ” for in

const obj = {
	name: '๊น€์ ํ”„',
	age: '4'
}
for (const key in obj) {
	console.log(`${key} : ${obj[key]}`);
}
// name : ๊น€์ ํ”„
// age : 4
  • ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ์ƒ์†๋ฐ›๋Š” ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ ์ƒ์˜ ๋ชจ๋“  property key๋ฅผ ์—ด๊ฑฐ (์˜ˆ์‹œ์—์„œ๋Š” name, age)

 

โœ” for of

const cat = ['๊น€์ ํ”„', '๊น€๋ณ‘์ฒ ', '๋ฒ„์ฐŒ'];
for (const c of cat) {
	console.log(c); // ๊น€์ ํ”„, ๊น€๋ณ‘์ฒ , ๋ฒ„์ฐŒ ์ถœ๋ ฅ
}

 

  • iterable ์ˆœํšŒ ์ „์šฉ (String, Array, Map, Set, Dom์ปฌ๋ ‰์…˜ ๋“ฑ)
  • ๊ฐœ์ธ์ ์œผ๋กœ ํŒŒ์ด์ฌ์˜ for๋ฌธ์ด๋ž‘ ๋น„์Šทํ•˜๊ฒŒ ์ƒ๊ฒผ๋‹ค๊ณ  ์ƒ๊ฐ (for a in arr: )

 

โœ” while

let num = 0;
while (num<10) {
	console.log(num); //0~9 ์ถœ๋ ฅ
	num++;
}
  • ๋ชจ๋‘๊ฐ€ ์•„๋Š” ๊ทธ while๋ฌธ

 

โœ” do while

do {
	console.log("๋ฌด์กฐ๊ฑด ํ•œ๋ฒˆ์€ ์‹คํ–‰ํ•จ");
} while (false);
  • ๋ฌด์กฐ๊ฑด ํ•œ๋ฒˆ์€ ์‹คํ–‰๋˜๋Š” ๊ทธ while๋ฌธ
  • ๊ทธ ๋‹ค์Œ์—๋„ while๋ฌธ ์กฐ๊ฑด์— ๋งž์œผ๋ฉด ์‹คํ–‰, ์•„๋‹ˆ๋ฉด ๊ด€๋‘๊ณ  ๋‚˜์˜ด

 

โœ” Object ๊ฐ์ฒด ๋ฉ”์„œ๋“œ (๊ฐ์ฒด ์ˆœํšŒ ์ „์šฉ)

Object.keys( {name: '๊น€์ ํ”„', age: '4'} ); // ['name', 'age']
Object.values( {name: '๊น€์ ํ”„', age: '4'} ); // ['๊น€์ ํ”„', '4']
Object.entries( {name: '๊น€์ ํ”„', age: '4'} ); // [ ['name', '๊น€์ ํ”„'], ['age', '4'] ]
  • Object.keys(๊ฐ์ฒด) → key๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
  • Object.values(๊ฐ์ฒด) → value๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
  • Object.entries(๊ฐ์ฒด) → [key, value]๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ 

 

โœ” Array.prototype ๋ฉ”์„œ๋“œ (๋ฐฐ์—ด ์ „์šฉ)

// 1. forEach
['๊น€์ ํ”„', '๊น€๋ณ‘์ฒ ', '๋ฒ„์ฐŒ'].forEach( (value, index, array) => {
	console.log(value); // ๊น€์ ํ”„, ๊น€๋ณ‘์ฒ , ๋ฒ„์ฐŒ ์ถœ๋ ฅ
} )

// 2.map
const map_result = [1,2,3,4].map( (value, index, array) => {
	console.log(value); // 1,2,3,4 ์ถœ๋ ฅ
	return value*10; //10, 20, 30, 40์„ ๋ฐ˜ํ™˜
} )
console.log(map_result); //[10,20,30,40]

//3. filter
const filter_result = [1,2,3,4].filter( (value, index, array) => {
	console.log(value); // 1,2,3,4 ์ถœ๋ ฅ
	return value > 2; // 2๋ณด๋‹ค ํฐ ์ˆซ์ž๋งŒ return
} )
console.log(filter_result); //[3,4]

// 4. reduce 
const reduce_result = [1,2,3,4].reduce( (pv, cv, index, arr) => {
	return pv + cv; //์ด์ „ ๋ฆฌํ„ด๊ฐ’ + ํ˜„์žฌ ๊ฐ’ (๋ˆ„์ ์˜ ๊ฐœ๋…)
}, 100); //์ดˆ๊ธฐ๊ฐ’ = 100
console.log(reduce_result); //100(์ดˆ๊ธฐ๊ฐ’) +1+2+3+4 = 110์ด ๋‚˜์˜ด

1. ๋ฐฐ์—ด.forEach( (value, index, array) => { ... } )

  • ๋ฐฐ์—ด ์ˆœํšŒ ์ „์šฉ
  • ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ value(์š”์†Œ๊ฐ’), index(์ธ๋ฑ์Šค), array(์›๋ณธ๋ฐฐ์—ด)๊ฐ€ ๋“ค์–ด์˜ด

 

2. ๋ฐฐ์—ด.map( (value, index, array) => { ... } )

  • forEach์™€ ๊ฑฐ์˜ ๋น„์Šทํ•จ
  • ๊ฐ ์ฝœ๋ฐฑํ•จ์ˆ˜์—์„œ returnํ•˜๋Š” ๊ฐ’๋“ค๋กœ ์ƒˆ ๋ฐฐ์—ด์„ ๋งŒ๋“ฌ!!!!

 

3. ๋ฐฐ์—ด.filter( (value, index, array) => { ... } )

  • forEach์™€ ๋น„์Šทํ•˜๊ณ ์š”
  • returnํ•˜๋Š” ๊ฐ’์ด true์ผ ๋•Œ์—๋งŒ, ๊ทธ value๋ฅผ ๋ฐ˜ํ™˜ → ๊ทธ๊ฑธ ๋ชจ์•„์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ์ƒ์„ฑ ํ›„ return

 

4. ๋ฐฐ์—ด.reduce( (previousValue, currentValue, currendIndex, array)=>{ ... }, initialValue )

  • initialValue๋ฅผ ์‹œ์ž‘๊ฐ’์œผ๋กœ ์ง€์ •
  • ๊ฐ ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ returnํ•˜๋Š” ๊ฐ’์ด ๋‹ค์Œ์— ์‹คํ–‰๋˜๋Š” ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ previousValue๋กœ ๋“ค์–ด๊ฐ
  • ์ตœ์ข…์ ์œผ๋กœ ๋งˆ์ง€๋ง‰ ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ returhํ•˜๋Š” ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•จ

 

 

๐Ÿ‘€์ฐธ๊ณ  (๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค)

๋”๋ณด๊ธฐ

https://curryyou.tistory.com/202

 

[์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] ๋ฐ˜๋ณต๋ฌธ ์ด์ •๋ฆฌ: for in, for of, forEach ๋“ฑ

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฐ˜๋ณต๋ฌธ์„ ์ •๋ฆฌํ•ด๋ณธ๋‹ค. # ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฐ˜๋ณต๋ฌธ ์ข…๋ฅ˜ 1. for : ๊ณ ์ „์ ์ธ for๋ฌธ 2. for in : ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ ํ‚ค ์—ด๊ฑฐ ์ „์šฉ 3. for of : ์ดํ„ฐ๋Ÿฌ๋ธ” ์ˆœํšŒ ์ „์šฉ 4. forEach(): ๋ฐฐ์—ด ์ˆœํšŒ ์ „์šฉ ๋ฉ”์„œ๋“œ

curryyou.tistory.com

 

https://7942yongdae.tistory.com/49

 

Javascript - Array filter ์‚ฌ์šฉ๋ฒ•

์ด๋ฒˆ์—๋Š” Javascript์˜ array๊ฐ€ ๊ฐ€์ง„ filter ํ•จ์ˆ˜์˜ ์ •์˜์™€ ์‚ฌ์šฉ๋ฒ• ๋”๋ถˆ์–ด ํ™œ์šฉ๋ฒ•์— ๋Œ€ํ•ด ์ด์•ผ๊ธฐํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. array์˜ ํ•จ์ˆ˜ ์ค‘ ๊ฐ€์žฅ ๋งŽ์ด ์“ฐ์ด๋Š” ํ•จ์ˆ˜ ์ค‘ 3 ๋Œ€์žฅ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด map, filter, reduc

7942yongdae.tistory.com

 

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/for...in

 

for...in - JavaScript | MDN

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

developer.mozilla.org

 

 

๋Œ“๊ธ€