【问题标题】:lodash search with startsWith function带有startsWith函数的lodash搜索
【发布时间】:2017-07-21 08:29:17
【问题描述】:

我有一组错误代码,我必须为其显示自定义消息。我正在尝试使用 lodash startsWith 函数查找匹配错误。在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);

【问题讨论】:

    标签: javascript ecmascript-6 lodash


    【解决方案1】:

    两个问题:

    • 拼写错误:你想要response.errors.errors(你的'r'放错了位置)
    • _.some 的结果没有返回:在它前面加上return

    这会给你结果。

    【讨论】:

      猜你喜欢
      • 2015-10-23
      • 1970-01-01
      • 2013-03-21
      • 2016-08-26
      • 1970-01-01
      • 1970-01-01
      • 2019-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多