Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
Create a string from array of operators and operands

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

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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));

about 4 years ago · Juan Pablo Isaza Report

0

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));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!