Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering result

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) {}
    };
like image 585
Lijo John Avatar asked Apr 26 '26 07:04

Lijo John


1 Answers

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)
like image 126
Yosvel Quintero Arguelles Avatar answered Apr 27 '26 19:04

Yosvel Quintero Arguelles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!