본문 바로가기
개발관련

javascript object find하기 초간단 예제

by 딸기별땅 2020. 9. 11.
private list = [] //타입지정 및 빈값

const code = 1

created(): {

 this.list = [
   { id: 0 , name: 'aa' },
   { id: 1 , name: 'bb' },
   { id: 2 , name: 'cc' },
 ]
 
}

const find = this.list.find((item: any) => {
        return item.id === code
      })

 console.log(find.name) // bb 
 
 //list라는 오브젝트에서 찾을것이다
 //배열을 item이라고 재정의 하겠다
 //배열의 결과값의 type은 굳이 정하지 않았다(string, number, object, array..)
 //item이라고 재정의한 object의 id가 code와 일치하는 결과를 return한다 
      

 

댓글