반응형
Angular 2 및 TypeScript로 두 개의 객체 배열을 병합 하시겠습니까?
이 주제에 대한 JavaScript 질문을 살펴 보았습니다.이 질문은 특히 TypeScript를 사용하는 Angular2에 관한 것입니다.
내가하려는 것은 json 객체를 배열에 연결하는 것입니다.
내 코드는 다음과 같습니다.
public results: [];
public getResults(){
this._service.get_search_results(this._slug, this._next).subscribe(
data => {
this.results.concat(data.results);
this._next = data.next;
},
err => {
console.log(err);
}
);
}
typescript 및 angular data.results
로 어떻게 연결할 수 this.results
있습니까?
this._slug
및 this._next
클래스에 설정됩니다.
감사.
차라리 다음을 사용해야한다고 생각합니다.
data => {
this.results = this.results.concat(data.results);
this._next = data.next;
},
로부터 concat
문서 :
concat () 메서드는 인수로 제공된 배열 및 / 또는 값과 결합 된 호출 된 배열로 구성된 새 배열을 반환합니다.
확산 운영자는 좀 냉각된다.
this.results = [ ...this.results, ...data.results];
확산 연산자를 사용하면 확장 된 버전의 배열을 다른 배열에 쉽게 배치 할 수 있습니다.
각도 6 확산 연산자 와 연결 이 작동하지 않습니다. 쉽게 해결할 수 있습니다.
result.push(...data);
ES6에서 권장하는 양식을 사용할 수도 있습니다.
data => {
this.results = [
...this.results,
data.results,
];
this._next = data.next;
},
이것은 배열을 먼저 초기화하면 작동합니다 ( public results = [];
); 그렇지 않으면 교체 ...this.results,
에 의해 ...this.results ? this.results : [],
.
도움이 되었기를 바랍니다
참고 URL : https://stackoverflow.com/questions/38092458/merge-two-object-arrays-with-angular-2-and-typescript
반응형
'IT TIP' 카테고리의 다른 글
UIScrollView 콘텐츠의 보이는 사각형 가져 오기 (0) | 2020.11.02 |
---|---|
프로젝트로드 오류 : 3 개의 패싯 세부 사항을로드 할 수 없습니다. (0) | 2020.11.02 |
Google지도 v3 : 최소 적용 (0) | 2020.11.02 |
Swift에서 NSNotification에 대한 관찰자를 제거하는 곳은 어디입니까? (0) | 2020.11.02 |
위치 서비스가 활성화되어 있는지 확인 (0) | 2020.11.02 |