반응형
hi.
today, I learned a new way for initialize the Map.
origin way is too long. like this :
for(let i = 0; i < x.length; i++){
if(a.has(x[i]){
a.set(x[i], a.get(x[i]) + 1)
} else { a.set(x[i],1)
}
for counting the all of the elements in some array, I usually use map and counting them.
but today, I found out shorten that code. like this :
for(let i = 0; i < x.length; i++){
a.set(x[i], a.get(x[i] || 0) + 1)
}
it will be working same with origin one.
for using '||' , I can get rid of the if statement.
if the value is undefined, it will be converted to 0,
fantastic!!
반응형
'열정가득한 개발자의 이야기 > 한땀 한땀 공부 내용 TIL' 카테고리의 다른 글
git command (0) | 2024.11.24 |
---|---|
Thread & service worker in JS (0) | 2024.11.14 |
TIL : 알고리즘 개념 정리 1 (feat. 코딩테스트 합격자되지 책) (0) | 2024.10.30 |
객체를 사용하는 이유 ? (0) | 2024.10.28 |
리액트 forwardRef 좀..정리 (1) | 2024.09.27 |