我从服务器获得此动态响应。在这里我想将mapD数据推送到mapData(即动态数据);
[
{
Name: "Aura",
OrderState: "2",
latitude: "28.197619999999997",
longitude: "65.24778333333334"
}
]
我将上面的动态响应存储为this.mapData = res.Data。
我也有静态数据。
public mapD:any=[{
Name: "mad",
OrderState: "2",
latitude: "39.6868",
longitude: "83.2185"
}]
我希望上面的静态对象包含动态对象,这样我总共得到2个对象 - 我使用push方法但无法使用
获取数据 this.mapD.push(this.mapData)
并使用mapData
保存响应并将其循环到模板中
答案 0 :(得分:1)
您无法将阵列推入另一个阵列。请改用spread
:
this.mapD.push(...this.mapData);
如果你不能使用ES6,请使用array.concat:
this.mapD = this.mapD.concat(this.mapData);
答案 1 :(得分:0)
您可以将mapD
声明为mapData
的数组,以便以后可以访问对象属性。一个简单的例子:
mapData: Array<any>
mapD: Array<any>
getResponse() {
this.yourApiService.subscribe(
(response: Array<any>) => {
this.mapDdata = response;
// Loop through the response array here or push first item
this.mapD.push(reponse[0]);
}
);
}
现在你有mapD
类型为array并包含映射的mapData
对象。这允许您访问存储在该阵列上的对象的属性。
答案 2 :(得分:0)
我不确定你的意思,但是如果你想将返回的数组mapData
合并到现有的mapD
中,你可以尝试以下方法:
Array.prototype.push.apply(this.mapD, this.mapData);
它会将结果存储在this.mapD
。
希望这有帮助,