['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
数组中。如何检查matchedComments
与searchText
的匹配项?
答案 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);
});
}
})
};