I have an empty object like this
let queryParams = {}
I want to add some objects conditionally to this:
if(status){
//add this
adminapproval:req.body.approvalStatus
}
if(type){
//add this
$or: [
Sequelize.where(Sequelize.fn('concat_ws', Sequelize.col('firstName'), ' ', Sequelize.col('lastName')), {
$like: '%' + keyword + '%'
}),
{ email: { $like: '%' + keyword + '%' } },
{ company: { $like: '%' + keyword+ '%' } },
{ mobileNo: { $like: '%' + keyword+ '%' } },
{ city: { $like: '%' + keyword+ '%' } },
{ country: { $like: '%' + keyword+ '%' } }
]}
If the two conditions are true the final object look like this:
{
adminapproval:req.body.approvalStatus,
$or: [
Sequelize.where(Sequelize.fn('concat_ws', Sequelize.col('firstName'), ' ', Sequelize.col('lastName')), {
$like: '%' + keyword + '%'
}),
{ email: { $like: '%' + keyword + '%' } },
{ company: { $like: '%' + keyword+ '%' } },
{ mobileNo: { $like: '%' + keyword+ '%' } },
{ city: { $like: '%' + keyword+ '%' } },
{ country: { $like: '%' + keyword+ '%' } }
]
}
My solution is to put this individual object into array and build the object in aloop but I think its a bad way. Is there any other way to do this?