我从API获得以下回报:
{
"0": {
"lat": "-30.02897288697400000",
"lng": "-51.22800440001900000"
},
"1": {
"lat": "-30.02879088697400000",
"lng": "-51.22803440001900000"
},
"2": {
"lat": "-30.02846288697400000",
"lng": "-51.22814140001900000"
},
"3": {
"lat": "-30.02822088697400000",
"lng": "-51.22821640001900000"
},
"4": {
"lat": "-30.02760388697400000",
"lng": "-51.22844040001900000"
}
(...continues)
如何将其映射到数组以在Angular应用中使用?到目前为止,我已经尝试过创建一个没有运气的对象数组:
export class MyClass {
lat: string;
lng: string;
}
和...
handleSuccessfulResponse(response) {
this.myClass = response;
}
我在做什么错了?
答案 0 :(得分:1)
您可以按以下方式使用方法Objects.values()
:
const result = {
"0": {
"lat": "-30.02897288697400000",
"lng": "-51.22800440001900000"
},
"1": {
"lat": "-30.02879088697400000",
"lng": "-51.22803440001900000"
},
"2": {
"lat": "-30.02846288697400000",
"lng": "-51.22814140001900000"
},
"3": {
"lat": "-30.02822088697400000",
"lng": "-51.22821640001900000"
},
"4": {
"lat": "-30.02760388697400000",
"lng": "-51.22844040001900000"
}
};
const myArray = Object.values(result);
console.log(myArray);
答案 1 :(得分:0)
您可以为此使用Object.values,查找代码段以获取更多详细信息。
let obj={
"0": {
"lat": "-30.02897288697400000",
"lng": "-51.22800440001900000"
},
"1": {
"lat": "-30.02879088697400000",
"lng": "-51.22803440001900000"
},
"2": {
"lat": "-30.02846288697400000",
"lng": "-51.22814140001900000"
},
"3": {
"lat": "-30.02822088697400000",
"lng": "-51.22821640001900000"
}}
let arr=Object.values(obj);
console.log(arr)