It is regarding filtering the result. Following code is working fine. But I would like to add one more field. video.description want to add along with 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) {}
};
You can do:
const result = videos.filter(v =>
['title', 'description'].some(prop =>
v[prop].toLowerCase().includes(req.params.word.toLowerCase()))
)
Code example:
// 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With