【发布时间】:2019-08-28 19:08:33
【问题描述】:
检查数组(步骤)对象中的输出是否有“错误”,并在“错误”中显示消息以及特定的 id。
如果 JavaScript 中存在错误,我想显示该错误。
这里是 JSON,
{
"steps": [
{
"start": null,
"stop": null,
"status": "PENDING",
"input": null,
"output": null,
"id": "45968631-4b24-4b80-a618-954ec383ce8d"
},
{
"start": "2019-08-23T00:11:27.323325Z",
"stop": "2019-08-23T00:11:50.581740Z",
"status": "SUCCESS",
"input": {
"url": "https://www.google.com"
},
"output": {
"filepath": "/tmp/filepath"
},
"id": "153eec8e-aff0-4566-9dee-bd2235f59886"
},
{
"start": "2019-08-23T00:26:31.377313Z",
"stop": "2019-08-23T00:26:58.489024Z",
"status": "SUCCESS",
"input": null,
"output": {
"url": "url"
},
"id": "cb2280a8-3b75-4e7b-9052-42a563b4fd9e"
},
{
"start": "2019-08-23T00:41:00.988154Z",
"stop": "2019-08-23T00:41:04.528278Z",
"status": "SUCCESS",
"input": {
"key": "userKey"
},
"output": {
"error": "ProcessorError"
},
"id": "65324ed2-d347-4a35-8fdc-fe11b98d5e70"
}
]
}
在某些地方,Output 没有“错误”,而有些地方则为 null。
【问题讨论】:
-
您进行了哪些研究或编写了哪些代码来实现这一目标?预计您在询问我们之前已尝试回答您自己的问题,否则您的问题只不过是一个代码请求。请编辑您的问题以包括您尝试完成自己的请求,以便我们可以帮助您调试。
-
@TylerRoper ,对于你的问题,我最初想到了这一点,它不符合我对输出的期望,是的,可能我需要了解更多关于过滤器和地图的知识。这是我尝试过的。
checkError(data){ console.log('ERROR', JSON.stringify(data, null, 2)); const vals = data.steps.filter( (ele) => { const message = ''; if(ele.output !== null) { this.message = ele.output.error; console.log('Message', message); } return message; });
标签: javascript arrays json object nested