【发布时间】:2020-09-09 22:43:41
【问题描述】:
嗨,我有一个从 api 返回的对象。
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"
}
},
null,
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon"
},
"properties": {
"color": 1,
"id": 12,
"stateId": 12,
"name": "Pondicherry",
"code": "PY"
}
},
null,
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon"
},
"properties": {
"color": 1,
"id": 1,
"stateId": 1,
"name": "Delhi",
"code": "DL"
}
}
]
}
在此数据中,在 ApiData1.features 中。有一些空值重新调整。我需要删除这个空值并且必须像这样返回。
输出
{
"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": 1,
"id": 1,
"stateId": 1,
"name": "Delhi",
"code": "DL"
}
}
]
}
无论如何都是通过将此 ApiData 传递给函数并像这样返回来实现这一点。你能帮我解决这个问题吗?
提前致谢
【问题讨论】:
-
这与您 32 分钟前提出的问题有关吗? stackoverflow.com/questions/61965838/…
-
有点,但不一样。
-
到目前为止你尝试过什么?可以看看
Array.filter方法 -
ApiData1.features = ApiData1.features.filter(o => o)
标签: javascript arrays reactjs javascript-objects