0

The following is JSON is generated from the Search Builder Plugin of Datatables and I would like to display the output in the following format using javascript without use of any library.

(Registration=1)OR((Registration=2)OR(Number=3))OR(Number=4)OR(Station=Yes)
var sbfilter = {
  "criteria": [
    {
      "condition": "=",
      "data": "Registration",
      "value": [
        "1"
      ]
    },
    {
      "criteria": [
        {
          "condition": "=",
          "data": "Registration",
          "value": [
            "2"
          ]
        },
        {
          "condition": "=",
          "data": "Number",
          "value": [
            "3"
          ]
        }
      ],
      "logic": "OR"
    },
    {
      "condition": "=",
      "data": "Number",
      "value": [
        "4"
      ]
    },
    {
      "condition": "=",
      "data": "Station",
      "value": [
        "Yes"
      ]
    }
  ],
  "logic": "OR"
};
2
  • Can you format in a better output than (Registration=1)OR((Registration=2)OR(Number=3))OR(Number=4)OR(Station=Yes)? Maybe an example of the output. Commented Mar 22, 2021 at 6:50
  • Hi Rifat, since this is a filter criteria generated JSON, i prefer to show the logic operators this way. Commented Mar 22, 2021 at 6:51

1 Answer 1

1

This is what I can think of right now, using a loop and a recursion to go through every nested array and make the query string. The algorithm can be improved.

var sbfilter = {
  "criteria": [
    {
      "condition": "=",
      "data": "Registration",
      "value": [
        "1"
      ]
    },
    {
      "criteria": [
        {
          "condition": "=",
          "data": "Registration",
          "value": [
            "2"
          ]
        },
        {
          "condition": "=",
          "data": "Number",
          "value": [
            "3"
          ]
        }
      ],
      "logic": "OR"
    },
    {
      "condition": "=",
      "data": "Number",
      "value": [
        "4"
      ]
    },
    {
      "condition": "=",
      "data": "Station",
      "value": [
        "Yes"
      ]
    }
  ],
  "logic": "OR"
};
var query = '';
function makeQuery(criteria, logic) {
  criteria.forEach((c, idx, array)=>{
    if (c.criteria) {
      // Add a parentheses since it's the start of a sub query
      query+='('
      makeQuery(c.criteria, c.logic)
      // Close the parentheses
      query+=')'
    } else {
      query+= '('+c.data+c.condition+c.value[0]+')'
    }
    // Check if not the last condition then add the logic to it
    if (idx != array.length - 1) {
      query+=logic
    }
  })
}
makeQuery(sbfilter.criteria, sbfilter.logic)
console.log(query)

Sign up to request clarification or add additional context in comments.

1 Comment

Worked perfect! Brilliant solution! Thank you.

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.