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
Maybe you can simplify using js map like this:
let alertTypesList = [{id: 9, name: "foo"},{id:8, name: "bar"}];
let acknowledgeByUserList = [{id:2, name:"foo"},{id:3, name:"bar"}];
let whereConditions = {
AND: [
{ OR: alertTypesList.map(o => ({alert_type_id: {equals: o?.id}})) },
{ OR: acknowledgeByUserList.map(o => ({acknowledged_by_user_id: {equals: o.id}})) }
]
};
console.log(`whereConditions: ${JSON.stringify(whereConditions)}`)
Base on your additional requirement in below comment here is the improvement:
let alertTypesList = [{ id: 9, name: "foo" }, { id: 8, name: "bar" }];
let acknowledgeByUserList = [{ id: 2, name: "foo" }, { id: 3, name: "bar" }];
function whereConditions(listAndId) {
return {
AND: listAndId
.map(o => ({
OR: o.list.map(i => ({ [o.id]: { equals: i?.id } }))
}))
.filter(o => (
o.OR.length > 0
))
}
}
// tests
let listAndId = [
{ list: alertTypesList, id: 'alert_type_id' },
{ list: acknowledgeByUserList, id: 'acknowledged_by_user_id' },
];
console.log(`${JSON.stringify(whereConditions(listAndId))}`);
// '{"AND":[{"OR":[{"alert_type_id":{"equals":8}},{"alert_type_id":{"equals":8}}]},{"OR":[{"acknowledged_by_user_id":{"equals":2}},{"acknowledged_by_user_id":{"equals":3}}]}]}'
listAndId = [
{ list: alertTypesList, id: 'alert_type_id' },
{ list: [], id: 'acknowledged_by_user_id' },
];
console.log(`${JSON.stringify(whereConditions(listAndId))}`);
// '{"AND":[{"OR":[{"alert_type_id":{"equals":9}},{"alert_type_id":{"equals":8}}]}]}'
listAndId = [
{ list: [], id: 'alert_type_id' },
{ list: acknowledgeByUserList, id: 'acknowledged_by_user_id' },
];
console.log(`${JSON.stringify(whereConditions(listAndId))}`);
// {"AND":[{"OR":[{"acknowledged_by_user_id":{"equals":2}},{"acknowledged_by_user_id":{"equals":3}}]}]}