2017-09-06 7 views
0

있는지 확인 나는 ownertenant이 사실이 두 속성 중 어느 것이 확인에 따라 this.productStatusReferences에 문자열을 할당이 조건이 사실이다 owner 여부 tenant자바 스크립트 - 속성의 값이 true와 리턴 문자열

if (this.projectCurrentProduct.occupancyType.owner) { 
    this.productStatusReferences = 'productStatusSale' 
} else if (this.projectCurrentProduct.occupancyType.tenant) { 
    this.productStatusReferences = 'productStatusRent' 
} 

예를 들어 Lodash 또는 es2015와 동일한 작업을 수행하는보다 정교한 솔루션을 찾고 있습니까?

+0

무엇을 묻고 있는지 확실하지 않습니다. 여기서 수정해야 할 부분이 많지 않습니다. 두 개의 if가 있기 때문에 삼항 항이 순서에 있지 않습니다. 스위치를 읽기가 더 쉬울 수 있습니다. – mplungjan

+0

둘 다 아닌 경우 어떻게해야합니까? – Bergi

+0

'this.projectCurrentProduct.occupancyType'에 대한 변수를 만들어야합니다. – Bergi

답변

0

여기에 개선해야 할 부분이 있으면 안전을 위해 lodash 경로 검사를 사용하고 체인이있는 경우 다른 것을 제거 할 수 있는지 잘 모르겠습니다. 그리고 이미 모두가 설정되지 않은 경우 당신이 경우 필요 코멘트에 언급

if (_.has(this.projectCurrentProduct, 'occupancyType.owner')) { 
    this.productStatusReferences = 'productStatusSale' 
} 
if (_.has(this.projectCurrentProduct, 'occupancyType.tenant')) { 
    this.productStatusReferences = 'productStatusRent' 
} 
if(_.isUndefined(this.productStatusReferences)) 

또 다른 개선은 당신이 그렇게

projectCurrentProduct: {occupancyType:'owner'} 

는 다음지도를 유지할 수있는 속성에 occupancyType을 변경할 수 있습니다 할 수 있다는 것입니다 종류를 선택하고 한 번에 설정하십시오.

var prodcutStatusOf = {owner: 'productStatusSale', tenant: 'productStatusRent'}; 
var occupancyType = this.projectCurrentProduct.occupancyType; 
this.productStatusReferences = prodcutStatusOf[occupancyType];