๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŒŒ | WEB DEV/React

[React] 5. ๋ฐ˜๋ณต๋˜๋Š” ๋ถ€๋ถ„ ์ถ•์•ฝํ•˜๊ธฐ (map)

by KASSID 2023. 5. 28.

๋ชฉ์ฐจ

    728x90

    ์ด์ „ ๊ธ€ โ–ผ

     

     

    html ์ž‘์„ฑ์‹œ ์–ด๋– ํ•œ ๋ถ€๋ถ„์—์„œ ํ…œํ”Œ๋ฆฟ์ด ๋ฐ˜๋ณต๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.

    ์ด๋Ÿฌํ•œ ๋ฐ˜๋ณต์„ ํšจ๊ณผ์ ์œผ๋กœ ์ค„์ด๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž.

     

    1. map ์•Œ์•„๋ณด๊ธฐ

    ๋“ค์–ด๊ฐ€๊ธฐ ์ „์— mapํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.

    array ์ž๋ฃŒํ˜•์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ์ด๊ณ  ๊ทธ ํ™œ์šฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

     

    https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/map

    arr.map((์š”์†Œ)=>{ ... })
    arr.map((์š”์†Œ, ์ธ๋ฑ์Šค)=>{ ... })
    
    
    arr.map(์ฝœ๋ฐฑํ•จ์ˆ˜)
    arr.map(์ฝœ๋ฐฑํ•จ์ˆ˜, thisArg)
    
    arr.map(function(์š”์†Œ){ ... })
    arr.map(function(์š”์†Œ, ์ธ๋ฑ์Šค){ ... })
    
    // ...etc

    ์ฝœ๋ฐฑํ•จ์ˆ˜๋กœ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜, function ๋ชจ๋‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    ๊ธฐ๋Šฅ

    1. array ๋‚ด ์š”์†Œ ๋“ค์— ๋Œ€ํ•ด ๊ฐ๊ฐ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. (์ฆ‰, ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋งŒํผ ์‹คํ–‰)

    2. ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด array์˜ ๊ฐ ์š”์†Œ๋ฅผ ๋„˜๊ฒจ์ค„ ์ˆ˜ ์žˆ๋‹ค.

    3. return ํ•  ๊ฒฝ์šฐ ๊ฐ ์š”์†Œ๋“ค์— ๋Œ€ํ•œ ์ฒ˜๋Ÿฌ ํ›„, ๊ทธ ๊ฐ’๋“ค์„ ๋ชจ์•„์„œ array์˜ ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

     

     

    ์˜ˆ์‹œ

    let arr = [1, 2, 3];
    
    arr.map(function(){
      console.log('A');
    });
    
    >>> A
    >>> A
    >>> A

     

    let arr = [1, 2, 3];
    
    arr.map(function(element){
      console.log(element);
    });
    
    >>> 1
    >>> 2
    >>> 3

     

    let arr = [1, 2, 3];
    
    let arr2 = arr.map(function(element){
      return element*10;
    });
    console.log(arr2);
    
    >>> [10, 20, 30]

     

     

    2. map ํ™œ์šฉํ•˜๊ธฐ

    ์œ„์—์„œ ์•Œ์•„๋ณธ map()์„ ํ™œ์šฉํ•˜์—ฌ ๋ฐ˜๋ณต๋˜๋Š” ์ฝ”๋“œ๋ฅผ ์ถ•์•ฝํ•ด๋ณด์ž.

    ...
    
    {
      Array.map(function(){
        return(
        
          ... // ๋ฐ˜๋ณต๋˜๋Š” html ๋ธ”๋ก
          
        )
      })
    }
    
    ...

    ์ด๋Ÿฌํ•œ ํ˜•ํƒœ๋กœ ์‚ฝ์ž…ํ•œ๋‹ค.

     

     

    ์˜ˆ์‹œ

    {
      music.map(function(a, i){ //a๋Š” ์š”์†Œ, i๋Š” ์ธ๋ฑ์Šค
        return(
          <div className="music-list" key={i} onClick={()=>{modal === 'closed' ? setModal('open'):setModal('closed')}}>
            <div className="music-type">
              <img className="music-type-img" src={setTypeImg(music[i].type)} alt="" />
              <span className="music-type-desc">
              {music[i].type}
              </span>
            </div>
            <div className="music-info-container">
              <div className="music-info">
                <h3 className="music-title">{music[i].title}</h3>
                <span className="music-singer">{music[i].singer}</span>
              </div>
              <div className="music-others-container">
                <div className="music-thumbs-container">
                  <div className="thumbs">
                    <span onClick={()=>{
                      updateThumbs(i);
                      }}>๐Ÿ‘ ์ข‹์•„์š”</span>
                  </div>
                  <span>{music[i].thumbs}</span>
                </div>
                <div className="music-stream-container">
                  <a href="."><img src={youtube} alt="" /></a>
                  <a href="."><img src={spotify} alt="" /></a>
                  <a href="."><img src={apple} alt="" /></a>
                </div>
              </div>
            </div>
          </div>
        )
      })
    }

     

    ์ฝœ๋ฐฑํ•จ์ˆ˜์— ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•˜๋ฉด

    ์œ„์ฒ˜๋Ÿผ ๋ฐฐ์—ด ๋‚ด ๊ฐ๊ฐ์˜ ์š”์†Œ์™€ ์ธ๋ฑ์Šค๋ฅผ ํ•จ์ˆ˜ ๋‚ด์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    โ€ป ์ฃผ์˜ํ•  ์  !

    <div className="music-list" key={i} ... >

    map()์œผ๋กœ ๋ฐ˜๋ณต์‹œํ‚ฌ ๋ธ”๋ก์— key={ i } ์™€ ๊ฐ™์€ ํ˜•ํƒœ์˜

    ํ‚ค ์†์„ฑ์„ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.

    ์ด๋ฅผ ํ†ตํ•ด์„œ React๊ฐ€ ๊ฐ๊ฐ์˜ div๋“ค์„ ๊ตฌ๋ถ„ํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.

     

     


    - ๋งŒ์•ฝ for๋ฌธ์„ ํ™œ์šฉํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ

    ๋”๋ณด๊ธฐ

    for๋ฌธ์€ JSX ์•ˆ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. ๋”ฐ๋ผ์„œ return() ๋ฐ–์—์„œ ์ž‘์„ฑํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.

     

    ๋‹จ๊ณ„
    1. html ๋ธ”๋ก ๋‹ด์„ array ์ƒ์„ฑ
    2. for๋ฌธ์„ ํ†ตํ•ด array์— html๋ธ”๋ก push
    3. ์‚ฝ์ž…ํ•  ์œ„์น˜์— { array }

     

    ์˜ˆ์‹œ

    function App() {
      let arr = [];
      for (let i=0; i<5; i++){
        arr.push(์ถ”๊ฐ€ํ•  html ๋ธ”๋ก)
      }
      return(
        <>
          { arr }
        </>
      )
    }

     

    ๋Œ“๊ธ€