gpt4 book ai didi

javascript - lodash 使用startsWith 函数进行搜索

转载 作者:行者123 更新时间:2023-12-03 03:49:23 24 4
gpt4 key购买 nike

我有一组错误代码,必须为其显示自定义消息。我正在尝试使用 lodashstartsWith 函数查找匹配错误。在 codepen 中尝试过,但未定义。请问您能帮忙吗?

// An array, object or any data (eg. from an ajax call)
let errorCodes = ['ErrorCode1', 'ErrorCode2', 'ErrorCode3', 'ErrorCode4', 'ErrorCodeBusiness']

let response = {

meta:
{
version: "1.0",
time: "2017-07-20 08:28 AM GMT"
},

errors:
{
errors:
[
{
id: null,
code: "ErrorCodeBusiness",
message: "",
uuid: ""
}
],
warnings: null
}

};

//Find the error code that starts with given set of errors
const hasError = _.find(response.errors.erorrs, (error) => {
_.some(errorCodes, (code) => _.startsWith(code,error.code))

})

console.log(hasError);

最佳答案

两个问题:

  • 拼写错误:您想要 response.errors.errors(您的“r”放错了位置)
  • 不返回_.some的结果:在其前面添加return

这会给你结果。

关于javascript - lodash 使用startsWith 函数进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45232795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com