Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- supabase 페이지네이션
- map
- 타입스크립트
- xlsx-js-style
- Filter
- 스크롤이벤트
- react
- meatadata
- array
- set
- supabase authentication
- javascript
- 상속
- supabase auth
- 페이지네이션
- Spring
- reactnative
- code-push-standalone
- interface
- 이진탐색
- 코드푸시
- extends
- app.post
- generic
- TS
- async
- Next.js
- 슬라이딩윈도우
- codepush
- 글또10기x코드트리
Archives
- Today
- Total
rhanziy
배열 중복 요소 제거, 객체 배열정렬 본문
filter를 이용한 배열의 중복 요소 제거
Uniq() {
return this.filter((v, i, self) => self.indexOf(v) === i)
}
객체 배열 정렬
const todos = [
{id:4, content: 'JS'},
{id:1, content: 'HTML'},
{id:2, content: 'CSS'}
]
//배열 key값 타입에 따라 정렬
function compare(key){
// 프로퍼티 값이 문자열일 경우 산술연산으로 비교하면 NaN이 나오므로 비교 연산 사용
// 비교함수는 양수/음수/0을 반환하면 되므로 산술연산 대신 비교연산을 사용할 수 있다.
return (a,b) => typeof a[key] === 'string' ? a[key].localeCompare(b[key], 'ko-KR') : a[key] - b[key]
}
todos.sort(compare('id'));
todos.sort(compare('content'));

'Html_css_js' 카테고리의 다른 글
슬라이딩 윈도우 알고리즘 (0) | 2024.09.10 |
---|---|
이진탐색 알고리즘 (1) | 2024.09.10 |
재귀함수 (0) | 2024.09.10 |
js - jquery 동적으로 생성한 버튼 기능구현(delete) (0) | 2023.09.06 |
js - 선택한 이미지 미리보기 구현 (2) | 2023.09.06 |