【发布时间】:2020-09-09 11:13:29
【问题描述】:
这里我有一个对象,即 ApiData1 。它在 properties 中有颜色键值对。我正在根据 ApiData2 值 numberOfProjects 更改颜色值,并且有一个 numberOfProjects 值位于一组范围之间的范围,我正在更新颜色值。它工作正常。
在某些情况下,ApiData2 值会为 null。在这种情况下,它必须返回已经存在的默认值,即 ApiData1 中存在的值。但根据我的代码,它删除了 value 。我不知道如何解决这个问题。请帮我解决这个问题。我在这里分享工作演示链接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)
非常感谢您的帮助或建议。
提前致谢。
结果:
在 ApiData1 中,马哈拉施特拉邦的颜色值为 4 。在 ApiData2 中,马哈拉施特拉邦的 latestMetric 为空。如果我在 ApiData1 和 ApiData2 中删除此 Maharashtra 值。代码工作正常并更新颜色值。但是如果我在这种情况下运行代码,它会破坏代码。
我想要做的是,如果 ApiData2 值返回 null,我只需要传递 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": 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"
}
},
]
}
【问题讨论】:
-
我正在研究与此类似的东西。我会尽快为你写一个答案。您能否更新问题以提供您正在寻找的确切输出?
-
我已经编辑了我的问题。请看一下
-
通过 ApiData2 值 null 你的意思是整个 ApiData2 数组将为空?
-
没有。如果您在 ApiData2 中看到第四个对象,则 latestMetric 为空。在这种情况下,ApiData1 值,即第四个对象将保持不变。
标签: javascript arrays reactjs javascript-objects