Looking for a way to dynamically create objects in the 'map' function to reduce the final array size. The goal is to avoid adding properties with undefined values. For example, if 'offst: undefined', skip this property but add others with proper integer values. Is there a way to achieve this without looping through the array again? The array is quite large with over 150k entries, so performance is a concern.
this.mapData = this.mapData
.map((e) => ({
p: e.p,
lat: e.lat,
lng: e.lng,
y: Object.keys(e.values)[0],
ct: Object.values(e.values)[0].ct, // include ct: only if not undefined
cp: Object.values(e.values)[0].cp,
w: Object.values(e.values)[0].wp,
offst: Object.values(e.values)[0].offst,
onst: Object.values(e.values)[0].onst,
ertn: Object.values(e.values)[0].ertn,
ertl: Object.values(e.values)[0].ertl,
dst: Object.values(e.values)[0].dst,
ft: Object.values(e.values)[0].ft,
}))
.filter((item) => item.chargersTotal !== 0);
return this.mapData;
Sample JSON data:
[
{
"p":"BA1 2RU",
"lat":"51.38934",
"lng":"-2.364467",
"values":{
"2019":{
"ct":0.0
"dst":5.0
"onst":1.0
}
}
},
{
"p":"BA10 0AA",
"lat":"51.112275",
"lng":"-2.453865",
"values":{
"2019":{
"offst":1.0,
"ct":1.0
}
}
},
{
"p":"BA10 0AB",
"lat":"51.112463",
"lng":"-2.454067",
"values":{
"2019":{
"ct":0.0
}
}
}
]