1

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)}`)

codesandbox

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

1 Answer 1

1

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}}]}]}
Sign up to request clarification or add additional context in comments.

2 Comments

i think i forgot to mentioned that in the event that the array is empty, it would not return an empty OR array
I've improve the code to filter empty OR

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.