【问题标题】:Check if output in the Object of arrays has 'error' and display the message in the 'error'检查数组对象中的输出是否有“错误”并在“错误”中显示消息
【发布时间】: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


【解决方案1】:

您可以使用filter()map() 方法轻松实现此目的。

const obj = {
    "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"
    }]
}

const result = obj.steps.
filter((r) => {
    return r.output && r.output.error;
}).map((m) => {
    return {
            id: m.id,
            error: m.output.error
    }
});
console.log(result);

【讨论】:

    【解决方案2】:

    不完全确定您想在这里做什么,但这是您可以检查每个错误并采取相应措施的方法。

    let data={"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"}]}
    
    data.steps.forEach(item => {
      //  Check for output's existence then an error's existence
      if (!!item.output && !!item.output.error) {
        console.log(item.output.error);
      }
    });

    【讨论】:

    • 如果“output.error”是一个空字符串,这段代码会说它不存在。最好使用if ("output" in item && "error" in item.output)
    • @HereticMonkey 提示说输出可能为空或对象上不存在错误。我离开了,解决了那些案件。虽然是,但它不考虑空字符串。
    【解决方案3】:

    将您的数据保存在变量var data 中并应用这句话

    data.steps.filter(x=>x.output && x.output.error).map(result=>({id:result.id,errro:result.output.error}))
    

    【讨论】:

    • 如果“output.error”是一个空字符串,这段代码会说它不存在。最好使用"output" in x && "error" in x.output
    猜你喜欢
    • 2012-09-07
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-13
    相关资源
    最近更新 更多