자바스크립트 Map과 {} 사용법 차이

dyeon-dev·2025년 6월 11일

자바스크립트

목록 보기
8/10

Map 사용

const map = new Map();

  const ratio = [1, 1/2, 2/3, 3/4]

    weights.sort((a,b)=>a-b)
    for(x of weights) {
        for(y of ratio) {
            if(map.has(x*y)) answer+=map.get(x*y)
        }
        map.set(x, **(map.get(x)||0)+1)**
    }

map.set(x, (map.get(x)||0)+1) <- 괄호에 주의하자

{} 사용

 weights.sort((a,b)=>a-b)
    for(x of weights) {
        for(y of ratio) {
            if(dic[x*y]) answer+=dic[x*y]
        }
       
        if(!dic[x]) dic[x]=1
        else dic[x]++;
    }

{} 사용법도 똑같이 key-value 쌍을 저장해주는 것이다.
어쩌면 더 간편할지도?

profile
https://github.com/dyeon-dev

0개의 댓글