TIL 220803

HaByungNoยท2022๋…„ 8์›” 3์ผ
0

Today I Learned

๋ชฉ๋ก ๋ณด๊ธฐ
6/16
post-thumbnail

Done ๐Ÿ˜


Learned ๐Ÿ‘Œ

URI & URL & URN ๐Ÿ‘ˆ ์ •๋ฆฌ๊ธ€


javascript - Array.prototype.find()

find() ๋ฉ”์„œ๋“œ๋Š” ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ์š”์†Œ๊ฐ€ ์—†๋‹ค๋ฉด undefined๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

ํ™œ์šฉ๋ฒ•1 : find(์กฐ๊ฑด) ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” element ์ฐพ๊ธฐ
const array1 = [5, 12, 8, 130, 44];

const found = array1.find(element => element > 10);

console.log(found);
// expected output: 12

ํ™œ์šฉ๋ฒ•2 : ์†์„ฑ ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์—์„œ ๊ฐ์ฒด ์ฐพ๊ธฐ
const inventory = [
    {name: 'apples', quantity: 2},
    {name: 'bananas', quantity: 0},
    {name: 'cherries', quantity: 5}
];

const result = inventory.find(fruit => fruit.name === 'cherries');

console.log(result) // { name: 'cherries', quantity: 5 }

๋ฆฌ์•กํŠธ์—์„œ ํ™œ์šฉํ•œ ์˜ˆ
const Detail = () => {
    const todos = useSelector((state) => state.inputTodo.todos);
    const navigate = useNavigate();
    const param = useParams();
  
    const thisTodo = todos.find((e) => e.id === param.id) 
    // param์—์„œ ๋“ค์–ด์˜จ id๊ฐ’๊ณผ state ์ธ todos ์˜ id ๊ฐ’์ด๋ž‘ ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ์ฐพ๋Š”๋‹ค.
    
    const shoutedId = param.id.split('-')[0]

    return (
        <Background>
            <Modal>
                <Top>
                    <div>id : {shoutedId}</div>
                    <div><button onClick={() => navigate('/')}>์ด์ „์œผ๋กœ</button></div>
                </Top>
                <Bottom>
                    <h1>{thisTodo.title}</h1>
                    <div>{thisTodo.body}</div>
                </Bottom>
            </Modal>
        </Background>
    )
}

profile
ํ”„๋ผ๊ณ 

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