im looking to have an end result where i passed in array values based on a key to "OR" an OR array.
whereConditions = {
"AND": [
{
"OR": [
{
"alert_type_id": {
"equals": "9"
}
},
{
"alert_type_id": {
"equals": "8"
}
}
]
},
{
"OR": [
{
"acknowledged_by_user_id": {
"equals": "2"
}
},
{
"acknowledged_by_user_id": {
"equals": "3"
}
}
]
}
]
}
the data is coming from alertTypesList, acknowledgeByUserList which are arrays that store the respective data.
let alertTypesList = [{id: 9, name: "foo"},{id:8, name: "bar"}]
let acknowledgeByUserList = [{id:2, name:"foo"},{id:3, name:"bar"}]
currently im doing this which i dont think is very nice
let whereConditions = {
AND: []
}
let whereAlertConditions = []
let whereAckConditions = []
if(alertTypesList.length !=0){
for (let alertType of alertTypesList){
whereAlertConditions = [
...whereAlertConditions,
{alert_type_id: {equals: alertType?.id}},
];
}
}
if(acknowledgeByUserList.length !=0){
for (let acknowledgeByUser of acknowledgeByUserList){
whereAckConditions = [
...whereAckConditions,
{acknowledged_by_user_id: {equals: acknowledgeByUser.id}},
];
}
}
if(whereAlertConditions.length !=0){
whereConditions = {
AND: [
...whereConditions?.AND,
{
OR: whereAlertConditions,
},
],
};
}
if(whereAckConditions.length !=0){
whereConditions = {
AND: [
...whereConditions?.AND,
{
OR: whereAckConditions,
},
],
};
}
console.log(`whereConditions: ${JSON.stringify(whereConditions)}`)
is there a better way to format this? i tried to parse in spread of the object in the nested whereConditions but it couldnt spread at the correct level