Is there a way to use a sort of “adaptive variable name” in Javascript? [duplicate]

I’m new to javascript, and I’m working on a basic route handler set for a class of mine to perform CRUD operations on a MongoDB server. Our professor explained using something like the following to search through the database:

if(req.query.year !== undefined) {
        filter.year = req.query.year;
    }

This works great for one or two potential search querys, but with many it starts to look like this:

if(req.query.year !== undefined) {
        filter.year = req.query.year;
    }
    if(req.query.title !== undefined) {
        filter.title = req.query.title;
    }
    if(req.query.title !== undefined) {
        filter.title = req.query.title;
    }

Which is obnoxiously repetitive.

I tried doing something like this:

for (item in req.query) {
    if(req.query.item !== undefined) {
            filter.item = req.query.item;
    }
}

While this didn’t work, I’d imagine there’s some kind of simple way to fix this, but I can’t think of what it would be called (Which makes it very irritating to find a solution!) Any help would be appreciated! Thanks.