I want to add new properties in "gj" from "dataToAdd". The format of "gj is as:
const gj = {
"type": "FeatureCollection", "features" : [
{
"type": "Feature",
"geometry": {
"type": "Ploygon",
"coordinates": ["coordinates"]
},
"properties": {
"OBJECTID": 1,
"DS_ID" : 1
}
},
{
"type": "Feature",
"geometry": {
"type": "Ploygon",
"coordinates": ["coordinates"]
},
"properties": {
"OBJECTID": 2,
"DS_ID" : 3
}
},
{
"type": "Feature",
"geometry": {
"type": "Ploygon",
"coordinates": ["coordinates"]
},
"properties": {
"OBJECTID": 3,
"DS_ID" : 2
}
},
]
}
& the format of dataToAdd is as:
const dataToAdd = [
{
"ds_id": 3,
"value": 10
},
{
"ds_id": 1,
"value": 20
},
{
"ds_id": 2,
"value": 30
},
]
I want the require output in below format:
requireOutput = {
"type": "FeatureCollection", "features" : [
{
"type": "Feature",
"geometry": {
"type": "Ploygon",
"coordinates": ["coordinates"]
},
"properties": {
"OBJECTID": 1,
"DS_ID" : 1,
"value": 20
}
},
{
"type": "Feature",
"geometry": {
"type": "Ploygon",
"coordinates": ["coordinates"]
},
"properties": {
"OBJECTID": 2,
"DS_ID" : 3,
"value": 10
}
},
{
"type": "Feature",
"geometry": {
"type": "Ploygon",
"coordinates": ["coordinates"]
},
"properties": {
"OBJECTID": 3,
"DS_ID" : 2,
"value": 30
}
},
]
}
I am able to add the data in properties but I am having difficulty to achieve what I want:
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)
How can I add type & geometry? I know I am just lacking a small logic. I am not able to catch.
Try this
let requireOutput = JSON.parse(JSON.stringify(gj));// For Deep Cloning so that gj does not get changed
for (i of requireOutput.features)
i.properties.value = dataToAdd.find((item) => item.ds_id === i.properties.DS_ID).value
const gj = {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Ploygon",
"coordinates": ["coordinates"]
},
"properties": {
"OBJECTID": 1,
"DS_ID": 1
}
},
{
"type": "Feature",
"geometry": {
"type": "Ploygon",
"coordinates": ["coordinates"]
},
"properties": {
"OBJECTID": 2,
"DS_ID": 3
}
},
{
"type": "Feature",
"geometry": {
"type": "Ploygon",
"coordinates": ["coordinates"]
},
"properties": {
"OBJECTID": 3,
"DS_ID": 2
}
},
]
}
const dataToAdd = [{
"ds_id": 3,
"value": 10
},
{
"ds_id": 1,
"value": 20
},
{
"ds_id": 2,
"value": 30
},
]
let requireOutput = JSON.parse(JSON.stringify(gj)); // For Deep Cloning so that gj does not get changed
for (i of requireOutput.features)
i.properties.value = dataToAdd.find((item) => item.ds_id === i.properties.DS_ID).value
console.log(requireOutput);
Plus- If you want to edit your function, try this
let requireOutput = JSON.parse(JSON.stringify(gj))
for (let i = 0; i < requireOutput.features.length; i++) {
const properties = requireOutput.features[i].properties
requireOutput.features[i].properties = {
...properties,
...dataToAdd.find((item) => item.ds_id === properties.DS_ID),
}
}