javascript - 过滤结果
问题描述
它是关于过滤结果的。以下代码工作正常。但我想再添加一个字段。video.description 要与 video.title 一起添加
exports.getSearchvideo = async (req, res) => {
try {
const videos = await Video.find();
const index = videos.filter(
video =>
video.title
.toLowerCase()
.toString()
.indexOf(req.params.word.toLowerCase().toString()) > -1
// want to add video.description
);
res.send(index);
} catch (error) {}
};
解决方案
你可以做:
const result = videos.filter(v =>
['title', 'description'].some(prop =>
v[prop].toLowerCase().includes(req.params.word.toLowerCase()))
)
代码示例:
// API videos response
const videos = [{ title: 'Title for Video 1', description: 'Description', }, { title: 'Title for Video 2', description: 'Some description here' }]
const word = 'VIDEO 1'
const result = videos.filter(v =>
['title', 'description'].some(prop => v[prop].toLowerCase().includes(word.toLocaleLowerCase()))
)
console.log(result)
推荐阅读
- safari - Safari:加载资源失败:服务器响应状态为 421 ()
- sql-server - GUID 后的 SQL Server VSS 编写器编号
- python - Find_Peaks:图像数据的无效形状 (4951,)。可能的解决方案?
- javascript - Angular:iframe中的PDF如果太大则不会显示
- python - 如何连接两个张量
- json - 使用 AWS Rekognition 检测 PPE 并将结果存储在 DynamoDB 中
- glob - 试图从 metalsmith-simple-search 中排除特定文件
- prolog - prolog 一元数 - 评估表达式
- typescript - 为什么我需要在声明中使用类型断言来将变量分配给 null?
- php - PHP - 如何从多维数组生成非常规数据