In the "gj" object, I need to add new properties from the "dataToAdd" array. The current format of "gj" is as follows:
const gj = {
"type": "FeatureCollection", "features" : [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": ["coordinates"]
},
"properties": {
"OBJECTID": 1,
"DS_ID" : 1
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": ["coordinates"]
},
"properties": {
"OBJECTID": 2,
"DS_ID" : 3
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": ["coordinates"]
},
"properties": {
"OBJECTID": 3,
"DS_ID" : 2
}
},
]
}
The format of "dataToAdd" is:
const dataToAdd = [
{
"ds_id": 3,
"value": 10
},
{
"ds_id": 1,
"value": 20
},
{
"ds_id": 2,
"value": 30
},
]
The desired output format should be:
requireOutput = {
"type": "FeatureCollection", "features" : [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": ["coordinates"]
},
"properties": {
"OBJECTID": 1,
"DS_ID" : 1,
"value": 20
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": ["coordinates"]
},
"properties": {
"OBJECTID": 2,
"DS_ID" : 3,
"value": 10
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": ["coordinates"]
},
"properties": {
"OBJECTID": 3,
"DS_ID" : 2,
"value": 30
}
},
]
}
I have managed to add the data in the properties, however, I am struggling to achieve the desired output:
let requireOutput = [];
for(let i =0; i<gj.features.length; i++) {
const properties = gj.features[i].properties
requireOutput.push({
...properties,
...dataToAdd.find((item) => item.ds_id === properties.DS_ID)
})
}
console.log(requireOutput)
I need help in adding type and geometry. I believe I am missing a small logic. Can someone assist me?