Front-end/React

[React] ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ•ต์‹ฌ ๊ฐœ๋… - ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜

๐Ÿง ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜์ด๋ž€?

   ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์›๋ณธ์„ ๋ณ€๊ฒฝํ•œ ๋ณต์‚ฌ๋ณธ์„ ๋งŒ๋“ค์–ด์ค€๋‹ค.

        -> ์ด๋Ÿฐ ์‹์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ๋œ ๋ช…๋ นํ˜•์ด ๋˜๊ณ , ๊ทธ์— ๋”ฐ๋ผ ๋ณต์žก๋„๋„ ๊ฐ์†Œํ•œ๋‹ค

 

    ์ด๋•Œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์•ˆ์—๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€ํ™˜ํ•ด์„œ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด ๋‚ผ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๋„๊ตฌ๋“ค์ด ์žˆ๋‹ค.

 

1) Array.filter

  • ์›๋ณธ ๋ฐฐ์—ด๋กœ๋ถ€ํ„ฐ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด๋‚ด๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฐฐ์—ด ๋‚ด์žฅ ํ•จ์ˆ˜์ด๋‹ค.
  • ์ˆ ์–ด(predicate)๋ฅผ ์œ ์ผํ•œ ์ธ์ž๋กœ ๋ฐ›๋Š”๋‹ค. (์ˆ ์–ด : true๋‚˜ false๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜)
  • ๋ฐฐ์—ด์— ์žˆ๋Š” ๋ชจ๋“  ์›์†Œ์— ์ด ์ˆ ์–ด๋ฅผ ํ•œ ๋ฒˆ์”ฉ ํ˜ธ์ถœํ•œ๋‹ค.
  • ์ˆ ์–ด์— ๋ฐฐ์—ด์˜ ์›์†Œ๋ฅผ ์ธ์ž๋กœ ์ „๋‹ฌํ•˜๋ฉฐ, ๋ฐ˜ํ™˜ ๊ฐ’์ด true์ด๋ฉด ํ•ด๋‹น ์›์†Œ๋ฅผ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์— ๋„ฃ๋Š”๋‹ค.
const cities = cities.filter(city => city[0] === "W") //W๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋„์‹œ๋งŒ ๋ฝ‘์•„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ฃผ์ž.

 

  • ๋ฐฐ์—ด์—์„œ ์›์†Œ๋ฅผ ์ œ๊ฑฐํ•ด์•ผ ํ• ๋•Œ Array.pop์ด๋‚˜ Array.splice๋ณด๋‹ค๋Š” Array.filter๋ฅผ ์‚ฌ์šฉํ•˜์ž. (Array.filter๋Š” ์ˆœ์ˆ˜ ํ•จ์ˆ˜)
const cutCity = (cut, list) => list.filter(city => city !== cut)

 

2) Array.map

  • Array.map์€ ์ˆ ์–ด๊ฐ€ ์•„๋‹ˆ๋ผ ๋ณ€ํ™˜ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š”๋‹ค.
  • ํ•จ์ˆ˜๋ฅผ ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์›์†Œ์— ์ ์šฉํ•ด์„œ ๋ฐ˜ํ™˜๋ฐ›์€ ๊ฐ’์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์ƒˆ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค.
const seoulCities = cities.map(city => '${city} in Seoul')
//map ํ•จ์ˆ˜๋Š” ๊ฐ ๋„์‹œ ์ด๋ฆ„ ๋’ค์— Seoul์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. ์ด๋•Œ ์›๋ณธ cities ๋ฐฐ์—ด์€ ์•„๋ฌด ๋ณ€ํ™”๊ฐ€ ์—†๋‹ค.

const seoulCities = cities.map(city => ({name: city}))
//๋ฌธ์ž์—ด์„ ํฌํ•จํ•˜๋Š” ๋ฐฐ์—ด๋กœ๋ถ€ํ„ฐ ๊ฐ์ฒด๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค.

 

  • ๋ฐฐ์—ด์˜ ์›์†Œ ์ค‘ ํ•˜๋‚˜๋งŒ ๋ณ€๊ฒฝํ•˜๋Š” ์ˆœ์ˆ˜ ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•  ๋•Œ๋„ map์„ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ๋‹ค.
let cities = {
    { name: "์ค‘๋ž‘๊ตฌ"},
    { name: "๋™๋Œ€๋ฌธ๊ตฌ"},
    { name: "๋…ธ์›๊ตฌ"},
    { name: "๊ฐ•๋‚จ๊ตฌ"}
}

let updatedSchools = editName("๋™๋Œ€๋ฌธ๊ตฌ", "์„œ๋Œ€๋ฌธ๊ตฌ", cities)

const editName = (oldName, name, arr) =>
	arr.map(item => {
        if(item.name === oldName){
            return {
                ...item,
                name
            }
        }
    })
//editName ํ•จ์ˆ˜๋Š” ์›๋ณธ ๋ฐฐ์—ด์€ ๊ทธ๋Œ€๋กœ ๋‘” ์ฑ„ ํ•™๊ต ์ด๋ฆ„์ด ๋ฐ”๋€ ์ƒˆ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค.

์œ„์—์„œ๋Š” Array.map์— ์ „๋‹ฌํ•œ ๋ณ€ํ™˜ ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ 1๊ฐœ๋ฟ์ด์—ˆ์ง€๋งŒ, ๊ฐ ์›์†Œ์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ณ€ํ™˜ ํ•จ์ˆ˜์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋„˜๊ฒจ์ค„ ์ˆ˜ ์žˆ๋‹ค.

const editNth = (n, name, arr) =>
	arr.map((item, i) => (i === n) ?
            ({...item, name}) :
            item
            )
let updateCities2 = editNth(2, "์„œ๋Œ€๋ฌธ๊ตฌ", cities)
//2๋ฒˆ์งธ ์ธ์ž๋ฅผ ์„œ๋Œ€๋ฌธ๊ตฌ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.

 

3) Array.reduce

  • ๊ฐ์ฒด๋ฅผ ์ˆ˜, ๋ฌธ์ž์—ด, ๋ถˆ๋ฆฐ ๊ฐ’, ๊ฐ์ฒด, ์‹ฌ์ง€์–ด ํ•จ์ˆ˜๋กœ ๋ณ€ํ™”์‹œ์ผœ์ค€๋‹ค.
const ages = [20,3,45,23,100,52]

const maxAge = ages.reduce((max,age) => {
    if(age>max){
        return age
    } else{
        return max
    }
}, 0)
//์ˆ˜๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด์—์„œ ์ตœ๋Œ“๊ฐ’์„ ์ฐพ๋Š” ํ•จ์ˆ˜์ด๋‹ค. ๋ฐฐ์—ด์„ ํ•˜๋‚˜์˜ ์ˆ˜๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•˜๋ฏ€๋กœ reduce๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

 

  • ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ์›์†Œ๊ฐ€ ์•„๋‹Œ ๋งจ ๋งˆ์ง€๋ง‰ ์›์†Œ๋ถ€ํ„ฐ ์ถ•์•ฝ์„ ์‹œ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด Array.reduceRight๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ‘ Filter, Map, Reduce๋ฅผ ์ž˜ ์‚ฌ์šฉํ•ด ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜์„ ์ž˜ํ•ด์ฃผ์ž!

๋ฐ˜์‘ํ˜•