코드노트

lodash find, findIndex, romove 알아보기 본문

Code note/자바스크립트

lodash find, findIndex, romove 알아보기

코드노트 2022. 6. 8. 22:33
import _ from 'lodash';

const users = [
  { userId: '1', name: 'a'},
  { userId: '2', name: 'b'},
  { userId: '3', name: 'c'},
  { userId: '4', name: 'd'},
  { userId: '5', name: 'e'}
];
const foundUser = _.find(users, {name: 'a'}); // {userId: "1", name: "a"}

_.find

메소드 첫번째 인수는 배열데이터, 두번째 인수는 찾고자 하는 객체로 호출할 수 있다.

 

 

 

const foundUserIndex = _.findIndex(users, {name: 'a'}); // 0

_.findIndex

첫번째 인수는 배열데이터, 두번째 인수는 찾고자 하는 객체데이터를 호출하면 인덱스 번호를 출력한다.

 

 

_.remove(users, {name: 'a'});

_.remove

첫번째 인수는 배열데이터, 두번째 인수는 지우려는 객체데이터를 호출하면 지울수 있다.