这是输入数据集:
const data = {
TripResults: {
Depth: [ 577, 694, 810 ],
Speed: [ 150, 150, 150 ],
Frequency: [ 3, 3, 3 ]
},
SurveyResults: null
}
以下方法不注意空对象("SurveyResults": null
),它会引发错误。我想知道如何应对这种情况吗?
Object.entries(data).map(([name, properties]) => { /* do something */ })
答案 0 :(得分:2)
您可以添加过滤条件并检查值。
result = Object
.entries(data)
.filter(([_, v]) => v !== null) // add a filtering
.map(([name, properties]) => ({
答案 1 :(得分:1)
怎么样
const data = {"TripResults": {"Depth": [577,694,810],"Speed": [150,150,150],"Frequency": [3,3,3]},"SurveyResults": null}
Object.entries(data).map(([name, properties]) => {
if (properties) { // properties are not null
console.log(properties)
}
});