【发布时间】:2018-08-23 16:47:12
【问题描述】:
有一个来自 HTTP 的嵌套响应:
this.dataSet = Observable.of({
"insideData": [{
id: "1"
requiredData:[{
firstname:"Amy"
lastname: "Amy"
}, {
firstname:"John"
lastname: "John"
}]
}]
}}).map(mydata => mydata.insideData);
我将其包含在组件中的另一个 Observable 中,并尝试将其减少为 requiredData 数组的嵌套。
getItem(id) { //id here is needed to get the first loop matching the id
this.filteredData = this.dataSet.find(data => data.id ===id);
this.gettingRequiredData = this.filteredData.map(data => data[0].json())
.map(data => data.requiredData.json() || []);
}
在 HTML 中,我有一个迭代 requiredDataSet 的 ngFor 循环
<div *ngFor="let data of gettingRequiredData; index as i;">......</div>
在运行这段代码时,它会给出
ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
我不确定为什么选择对象而不是数组
【问题讨论】:
-
就目前而言,
this.dataSet是一个 Observable -
那么我应该做些什么改变以便它迭代