[TIL]21.06.21

๋ฐ•์ฃผํ™ยท2021๋…„ 6์›” 21์ผ
0

Today I Learned

๋ชฉ๋ก ๋ณด๊ธฐ
39/104

๐Ÿ‘จโ€๐Ÿ’ป ์˜ค๋Š˜ ๊ณต๋ถ€ํ•œ ๊ฒƒ

  • MDN์— ๋‚˜์˜ค๋Š” array, object ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด๋ดค๋‹ค.
  • each๋ฅผ ๊ตฌํ˜„ํ•œ ํ•จ์ˆ˜ = function(collections, interatee){
    	let result = [];
      if(Array.isArray(collections)){
          	let idx = 0;
        	  for(let el of collections){
                interatee(el, idx, collections);
                idx += 1;
              }
        }else{
          for(let key in collections){
          	interatee(key, collections[key]);
          }
        }
    }

    : ๋Œ€ํ‘œ์ ์œผ๋กœ each์„ ๊ตฌํ˜„ํ•ด๋ดค๋Š”๋ฐ each๋ฅผ ๊ตฌํ˜„ํ•œ ํ•จ์ˆ˜(๋ฐฐ์—ด๋˜๋Š”๊ฐ์ฒด, ํ•จ์ˆ˜), ์ด๋ ‡๊ฒŒ ์ธ์ž๊ฐ’์„ ๋ฐ›๋Š”๋‹ค.

    ๋ฐฉ๊ธˆ ๊ตฌํ˜„ํ•œ ._each์„ ์ด์šฉํ•˜์—ฌ map์„ ๊ตฌํ˜„ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.

    const sum = (num) => {
    	return num + 1;
    }
    
    
    _.map = function(arr, sum){
    	let result = [];
      	// ๋ฐฐ์—ด์˜ ์š”์†Œ์™€ ์ธ๋ฑ์Šค๊ฐ’์„ ์“ฐ๊ฒ ๋‹จ ์˜๋ฏธ
      	each๋ฅผ ๊ตฌํ˜„ํ•œ ํ•จ์ˆ˜(arr, function(el, idx){
          		result.push(sum(el));
        })
    }

    ๊ณ ์ฐจํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ each๋ฅผ ๊ตฌํ˜„ํ•œ ํ•จ์ˆ˜์—์„œ ๋‘๋ฒˆ์งธ์ธ์ž๊ฐ’์— ํ•จ์ˆ˜๋ฅผ ๋„ฃ์–ด์ฃผ๊ณ  ๊ทธ ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ each๋ฅผ ๊ตฌํ˜„ํ•œ ํ•จ์ˆ˜์—์„œinteratee(el, idx, collections)์— ์ธ์ž๋ฅผ ๋„ฃ์–ด์ค€ ๊ฒƒ์ฒ˜๋Ÿผ el์™€ idx์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ–ˆ๋‹ค.

    ๊ณ ์ฐจํ•จ์ˆ˜๋ฅผ ์ด๋ ‡๊ฒŒ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒ ๊ตฌ๋‚˜๋ž€ ๊ฑธ ์•Œ์•˜๋‹ค.

    profile
    ๊ณ ํ†ต์—†๋Š” ์„ฑ์žฅ์€ ์—†๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค....

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