객체배열 가지고놀기(2021.10.22)

김도형 (르베니아)·2021년 10월 22일
0

TIL

목록 보기
21/38

객체 배열 가지고놀기

const doit = [
  { id: 11, name: '일', group: '사용자' },
  { id: 47, name: '이', group: '관리자' },
  { id: 85, name: '삼', group: '사용자' },
  { id: 97, name: '사', group: '관리자' },
];

해당 객체 배열에서
filter
find

const res = doit.filter((it) => it.group.includes('관리자'));
const rep = doit.find((it)=> it.group.includes('관리자'));

필터는 해당함수에 의해 통과한 모든 객체를 뽑아 새로운 배열을 만듬
find는 해당 함수의 조건에 맞는 가장 첫번째 값을 리턴

[
  { id: 47, name: '이', group: '관리자' },
  { id: 97, name: '사', group: '관리자' }
]
{ id: 47, name: '이', group: '관리자' }

만약 검색기능을 이용한다면...

const rea = doit.filter(it => new RegExp('사용', "i").test(it.group))

이렇게 사용해보자

[
  { id: 11, name: '일', group: '사용자' },
  { id: 85, name: '삼', group: '사용자' }
]
profile
한다. 간다. 해낸다.

0개의 댓글