[JS 2일차]

최충열·2022년 8월 24일
0

배열데이터의 결합

import _ from 'lodash'

const usersA = [
  { userId: '1', name: 'Heropy' },
  { userId: '2', name: 'Neo'}
]

const usersB = [
  { userId: '1', name: 'Heropy' },
  { userId: '3', name: 'Amy'}
]
// 배열데이터 결합
const usersC = usersA.concat(usersB)
console.log('concat', usersC)
// 하나의데이터에서 중복되는 내용없이! 배열로 제작 (배열데이터 하나)
console.log('uniqBy', _.uniqBy(usersC, 'userId'))

// 합치기전, 여러개배열데이터 적어주고 고유화시킬 속성이름 적고 반환한다.( 배열데이터 여러개 )
const usersD = _.unionBy(usersA, usersB, 'userId')

console.log('unionBy', usersD)


const users = [
  { userId: '1', name: 'Heropy' },
  { userId: '2', name: 'Neo'},
  { userId: '3', name: 'Amy'},
  { userId: '4', name: 'Evan'},
  { userId: '5', name: 'Lewis'}
]
// users안에 'Amy'가 포함된 객체데이터 찾음
const foundUser = _.find(users, { name: 'Amy'})
// users 안에 'Amy'가 포함된 Index 추출
const foundUserIndex = _.findIndex(users, {name: 'Amy'})

console.log(foundUser)
console.log(foundUserIndex)

// user안에 'Heropy'가 포함된 객체데이터 제거
_.remove(users, {name: 'Heropy'})
console.log(users)
profile
프론트엔드가 되고싶은 나

0개의 댓글