gpt4 book ai didi

javascript - 过滤结果

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

这是关于过滤结果的。以下代码工作正常。但我想再添加一个字段。 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)

关于javascript - 过滤结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60612287/

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