如何将索引对象数组转换为普通对象
How to convert an array of indexed objects to a plain object
如何转换这种类型的数组:
place:
[
{
"_id": "xxxxx",
"loc": [
0: "xxx",
1: "xxx"
]
}
]
像这样:
place:
{
lat: "xxx"
lng: "xxx"
_id: "xxxxx"
}
使用javascript,我试过
let object = {};
place.forEach(element => {
object[element[0]] = element[1];
});
但这对我不起作用,我正在使用 angular 创建一个方法,我需要转换我正在显示的对象数组
假设完全无效
"loc": [
0: "xxx",
1: "xxx"
]
实际上只是
"loc": [
"xxx",
"xxx"
]
let place =
[
{
"_id": "xxxxx",
"loc": [
"xxx",
"xxx"
]
}
]
let { _id, loc: [lat, lng]} = place[0];
let output = { _id, lat, lng};
console.log(output);