2017-09-14 2 views
3

각도 2의 객체 배열을 반복하고 객체의 특정 키에 대한 문자열 길이 표시를 제한해야합니다.Typescript에서 객체의 배열을 반복하십시오.

this.productService.loadAllProducts(product).subscribe(data => { 
    if (this.authService.checkActiveSession(data)) { 
    if (data.success) { 
    //console.log(this.product_desc.substring(0,2)) 
     for(let i=0;i<data.products.length ;i++){ //How to properly iterate here!! 
     console.log(data.products[0].product_desc) 
     } 
     this.source.load(data.products); 
    } else { 
     console.log('Not binded'); 
    } 
    } 

});

예 :있는 내가 사용한 displaing 동안 }

내가 (말) 10 자에 prod_desc의 길이를 제한 할 필요가

this.product_desc.substring(0,10) 

답변

6

사용할 수를 내장 forEach 기능 배열. 이처럼

:

//this sets all product descriptions to a max length of 10 characters 
data.products.forEach((element) => { 
    element.product_desc = element.product_desc.substring(0,10); 
}); 

버전이 잘못 생각하지 않았다. 다음과 같이 보입니다.

for(let i=0; i<data.products.length; i++){ 
    console.log(data.products[i].product_desc); //use i instead of 0 
} 
관련 문제