angularjs:将文本与值数组匹配

时间:2018-06-25 10:46:53

标签: javascript

['100','2','3','4']

当执行查询时,我将得到refValueDocs这是数组。我想检查exports.searchcomments = function (req, res) { var searchText = !!req.params && req.params.searchtext || null; var matchedComments = []; ReferenceValue.find({ domaincode: 'DEPOSITCMNTS' }) .lean() .exec(function (error, refValueDocs) { if (!!error) { billingUtils.doErrorResponse({ error: 'ERRORS.REFUNDABLEAMOUNT' }, req, res, error); } else { for (var i = 0; i < refValueDocs.length; i++) { refValueDocs[i].valuedescription = new RegExp('^' + searchText, 'i') } } }) };refValueDocs[i].valuedescription是否匹配。匹配的内容也将被推入searchText数组中。如何检查matchedCommentssearchText的匹配项?

1 个答案:

答案 0 :(得分:0)

您可以使用filter过滤数组中的值,它将返回包含过滤对象的新数组。

exports.searchcomments = function (req, res) {
    var searchText = !!req.params && req.params.searchtext || null;
    var matchedComments = [];
    ReferenceValue.find({ domaincode: 'DEPOSITCMNTS' })
        .lean()
        .exec(function (error, refValueDocs) {
            if (!!error) {
                billingUtils.doErrorResponse({
                    error: 'ERRORS.REFUNDABLEAMOUNT'
                }, req, res, error);
            } else {
                matchedComments = refValueDocs.filter(function(doc){
                    var regex = new RegExp('^' + searchText, 'i');
                    return doc.valuedescription.match(regex);
                });
            }
        })
};