2016-09-01 4 views
0

ES6을 사용하여 객체를 가져 와서 내 변수에 값을 제공하는 함수 및 일부 변수를 정의하는 클래스가 있습니다. 이것은 반복적 인 것이므로이를 달성하기 위해 구조 조정을 사용할 수있는 방법이 있습니까?클래스 멤버에 대한 객체 소멸

class BasicDataMgmt { 

    public id: number; 
    public name: string; 
    public email: string; 
    public phone: string; 
    public type: string; 

    fetchData(data) { 
    this.id = data.id; 
    this.name = data.name; 
    this.email = data.email; 
    this.phone = data.phone; 
    this.type = data.type; 
    } 

} 

답변

1

이 unsanitized 데이터

fetchData(data) { 
    Object.assign(this, data); 
    } 

일 수있다. 또는

fetchData({ id, name, ... }) { 
    Object.assign(this, { id, name, ... }); 
    } 

위생 처리 된 데이터의 경우.

Lodash _.pick을 사용하면 여기에 유용합니다 Object.assign(this, _.pick(data, ['id', 'name', ...])).