728x90
🌲 Lodash 메소드
🚨 고유 값 찾기
📍 합쳐진 데이터에서 고유 값을 찾을 때
_.uniqBy(데이터, '속성')
✅ .concat()으로 병합 후 _.uniqBy()에서 내부 중복데이터를 제거하고 고유한 값만 남겨 새로운 데이터를 반환
import _ from 'lodash'
const userA = [
{ userId: '1', name: 'rose'},
{ userId: '2', name: 'vanilla'}
]
const userB = [
{ userId: '1', name: 'rose'},
{ userId: '3', name: 'coffee'}
]
const userC = userA.concat(userB) // 두 개의 배열 데이터 병합
console.log('concat', userC)
console.log('uniqBy', _.uniqBy(userC, 'userId')) // 중복 데이터 제거
📍 데이터를 합친 후 고유 값을 찾을 때
_.unionBy(데이터, 데이터, '속성')
import _ from 'lodash'
const usersA = [
{ userId: '1', name: 'rose'},
{ userId: '2', name: 'vanilla'}
]
const usersB = [
{ userId: '1', name: 'rose'},
{ userId: '3', name: 'coffee'}
const usersD = _.unionBy(usersA, usersB, 'userId') // 합친 후 고유값 내보내기
console.log('unionBy', usersD)
🚨 수많은 데이터 중 원하는 데이터를 찾거나 삭제할 때
_.find(인수1, 인수2)
_.findIndex(인수1, 인수2)
_.remove(인수1, 인수2)
import _ from 'lodash'
const users = [
{ userId: '1', name: 'rose'},
{ userId: '2', name: 'vanilla'},
{ userId: '3', name: 'coffee'},
{ userId: '4', name: 'latte'},
{ userId: '5', name: 'milk'}
]
const foundUser = _.find(users, { name: 'rose' }) //찾기
const founduserIndex = _.findIndex(users, { name: 'rose' }) //찾아서 인덱싱하기
console.log(foundUser)
console.log(founduserIndex)
_.remove(users, { name: 'rose' }) //제거하기
console.log(users)
728x90
'FE' 카테고리의 다른 글
JSON, js 데이터로 변환하기 / 로컬 스토리지 local storage에 데이터 삽입, 꺼내오기, 수정하기 setItem, getItem, removeItem (0) | 2021.11.13 |
---|---|
js 모듈 import와 export (0) | 2021.11.13 |
lodash cloneDeep / 얕은 복사와 깊은 복사 (shallow, deep copy) (0) | 2021.11.13 |
데이터 불변성과 가변성, 원시데이터와 참조형 데이터 (0) | 2021.11.13 |
전개연산자 Spread, 아이템을 전개하는 방법 (0) | 2021.11.13 |