Here i am having a Object i.e ApiData1 . Where it has color key value pair inside properties . I am changing the color values according to ApiData2 value numberOfProjects, and having a range where numberOfProjects values lies between a set of range i am updating the color value. It is working fine.
in some scenarios ApiData2 value will come as null. In this case it has to return the default value that is already ,the value present in ApiData1. But it removes the value , according to my code. I dont know how to fix this. Pls help me with this. Here i am sharing the working demo link JS_FIDDLE
let ApiData1 = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon"
},
"properties": {
"color": 1,
"id": 10,
"stateId": 10,
"name": "Tamil Nadu",
"code": "TN"
}
},
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon"
},
"properties": {
"color": 1,
"id": 11,
"stateId": 11,
"name": "Karnataka",
"code": "KA"
}
},
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon"
},
"properties": {
"color": 1,
"id": 12,
"stateId": 12,
"name": "Pondicherry",
"code": "PY"
}
},
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon"
},
"properties": {
"color": 6,
"id": 13,
"stateId": 13,
"name": "Maharashtra",
"code": "TT"
}
},
]
}
let ApiData2 = [
{
id: 10,
name: "Tamil Nadu",
code: "TN",
latitude: 29.9964948,
longitude: 81.6855882,
latestMetric: {
stateId: 10,
year: 0,
numberOfProjects: 1433,
}
},
{
id: 11,
name: "Karnataka",
code: "KA",
latitude: 21.9964948,
longitude: 82.6855882,
latestMetric: {
stateId: 11,
year: 0,
numberOfProjects: 3500,
}
},
{
id: 12,
name: "Pondicherry",
code: "PY",
latitude: 22.9964948,
longitude: 87.6855882,
latestMetric: {
stateId: 12,
year: 0,
numberOfProjects: 5500,
}
},
{
id: 13,
name: "Maharashtra",
code: "PY",
latitude: 22.9964948,
longitude: 87.6855882,
latestMetric: null
}
];
function updateColor() {
function updateProperties(colorJsonObject, colorValue) {
let updatedProperties = {
...colorJsonObject.properties,
color: colorValue
};
/* console.log(updatedProperties) */
return updatedProperties;
}
let range = [
{
"Minimum": 1,
"Maximum": 2000,
"color": 1
},
{
"Minimum": 2000,
"Maximum": 4000,
"color": 2
},
{
"Minimum": 4000,
"Maximum": 6000,
"color": 3
}
]
let newData = {
...ApiData1,
features: ApiData1.features.map(colorObject => {
const apiData = ApiData2.find(apiData => {
if (
colorObject.properties.stateId === apiData.latestMetric.stateId
) {
return true;
}
return false;
});
console.log(apiData)
let newValue;
range.forEach(i => {
if (
apiData.latestMetric.numberOfProjects >= i.Minimum &&
apiData.latestMetric.numberOfProjects <= i.Maximum
) {
let value = updateProperties(colorObject, i.color)
newValue = {...colorObject,properties:value}
}
});
return newValue;
})
}
return newData;
}
let colorValue = updateColor();
console.log(colorValue)
Your help or suggestion is much appreciated.
Thanks in advance.
Result:
In ApiData1 the color value in Maharashtra is 4 . In ApiData2 the latestMetric of Maharashtra is null. If i remove this Maharashtra value in both ApiData1 and ApiData2 . the code works fine and updating the color values. But if i run the code with this scenario , it breaks the code.
Here what i am trying to do is, if ApiData2 value has returned null, i just need to pass the default value that is already present in ApiData1 without updating it. The output has to be like this
Expected Output:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon"
},
"properties": {
"color": 1,
"id": 10,
"stateId": 10,
"name": "Tamil Nadu",
"code": "TN"
}
},
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon"
},
"properties": {
"color": 2,
"id": 11,
"stateId": 11,
"name": "Karnataka",
"code": "KA"
}
},
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon"
},
"properties": {
"color": 3,
"id": 12,
"stateId": 12,
"name": "Pondicherry",
"code": "PY"
}
},
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon"
},
"properties": {
"color": 6,
"id": 13,
"stateId": 13,
"name": "Maharashtra",
"code": "TT"
}
},
]
}