Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda