I am working with an array of indexed objects and my goal is to iterate through it in order to transform it into a new flattened array.
Here is the initial array of objects:
"attentionSchedules": [
{
"room": "1",
"schedules": [
{
"days": [
"SA",
"WE"
],
"_id": "6271xxxx",
"initialTimeStr": "12:00 am",
"finalTimeStr": "12:00 am",
"initialTime": "2022-05-03T06:00:00.000Z",
"finalTime": "2022-05-03T06:00:00.000Z"
}
],
"place": {
"loc": {
"type": "Point",
"coordinates": [
-88.03xxx,
15.49xxx
]
},
"_idPlace": "5d5ba845xxx",
"name": "Labs",
"address": "xxx"
},
"floor": 1
},
{
"room": "23",
"floor": 1,
"schedules": [
{
"days": [
"MO",
"TH",
"WE",
"YOU",
"FR",
"SA"
],
"_id": "62754264a627af5fc44286b3",
"initialTimeStr": "08:00 am",
"finalTimeStr": "09:00 pm",
"initialTime": "2022-05-06T14:00:00.000Z",
"finalTime": "2022-05-07T03:00:00.000Z"
}
],
"place": {
"loc": {
"type": "Point",
"coordinates": [
-88.02xxx,
15.50xxx
]
},
"_idPlace": "ba",
"name": "Labs",
"address": "xx"
}
}
],
The desired output object looks like this:
{
lng: -88.02xxx,
lat: 15.50xxx,
_idPlace: "ba"
}
.
.
.
N
I attempted to achieve this by implementing the following method using Angular and JavaScript/Typescript:
let locCoord: any[] = [];
this.attentionSchedules?.forEach(elm => {
for (const [key, value] of Object.entries(elm.place.loc)) {
let lng = value[0];
let lat = value[1];
let dataObjLoc = {
_id: elm.place._id,
lat: lat,
lng: lng
}
locCoord.push(dataObjLoc);
}
});
console.log(locCoord);
However, the current output is not as expected since Object.entries duplicates the keys. Can anyone assist me in solving this issue? Thank you.