Home > Net >  Create a string from array of operators and operands
Create a string from array of operators and operands

Time:07-09

I am trying to make a tool to create MySQL query with javascript. So I have separate input boxes to fill the operator and operands. The example array looks like this.

var array = [ "OR", ["<", "col1", "col2"], [ "AND", ["==", "col3", "col4"], ["!=", "colx", "coly"]]];
var array2 = [ "OR", ["<", "col1", "col2"], [ "AND", ["==", "col3", "col4"], ["!=", "colx", "coly"], ["<", "colo", "othercol"]]];

the expected out put is

"col1 < col2 OR (col3 == col4 AND colx != coly)"
"col1 < col2 OR (col3 == col4 AND colx != coly AND colo < othercol)"

What I am trying is something like this.

function createString(array) {
    var string = '';
    for(var i = 0; i < array.length; i  ) {
        if(Array.isArray(array[i])) {
            //console.log(array[i]);
            if(array[i].length == 3) {
                var operator = array[i][0];
                var variable1 = array[i][1];
                var variable2 = array[i][2];
                string = variable1 operator variable2;
            }else {
                //console.log('length is ' array[i].length);
            }
            
            //is array
        }else {
            //string
            //console.log('it is string '  array[i]);
            string =array[i];
        }
    }
    
    return string;
}

But I don't think I am trying the right algorithm. I am still trying this and I will keep my question updated with each try.

thanks in advance

CodePudding user response:

You can try to convert it using below recursive approach:

const array = [ "OR", ["<", "col1", "col2"], [ "AND", ["==", "col3", "col4"], ["!=", "colx", "coly"]]];

const convert = arg => {
  const [operator, operand1, operand2] = arg;
  const processOperand = op => Array.isArray(op) ? `(${convert(op)})` : op;
  return `${processOperand(operand1)} ${operator} ${processOperand(operand2)}`;
}

console.log(convert(array));

EDIT: for your second example you can use below function:

var array2 = [ "OR", ["<", "col1", "col2"], [ "AND", ["==", "col3", "col4"], ["!=", "colx", "coly"], ["<", "colo", "othercol"]]];

const convert = arg => {
  const [operator, ...operands] = arg;
  const processOperand = op => Array.isArray(op) ? `(${convert(op)})` : op;
  return operands.reduce((prev, next) => `${processOperand(prev)} ${operator} ${processOperand(next)}`);
}

console.log(convert(array2));

CodePudding user response:

With your current and new examples, you can do like below:

  • Take out the first element of the array since it will always be an operator.

  • Do processing recursively on all the elements of the array.

  • Join all the sub/intermediate results with first operand of the array as the glue.

  • Don't worry about braces(or you can add one in the join() part below if you like) since AND has a precedence over OR.

var array = [ "OR", ["<", "col1", "col2"], [ "AND", ["==", "col3", "col4"], ["!=", "colx", "coly"], ["<", "colo", "othercol"]]];

function createString(element) {
    if(!Array.isArray(element)) return element;
    let results = [];
    for(let i = 1; i < element.length;   i){
      results.push(createString(element[i]));
    }
    return results.join(" "   element[0]   " ");
}

console.log(createString(array));

  • Related