用Javascript评估表达式树 [英] Evaluate expression tree in Javascript

查看:55
本文介绍了用Javascript评估表达式树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的输入包含嵌套的逻辑表达式对象

I have input consisting of nested logical expression objects

例如:

var obj = {
  'OR': [
      {
        'AND': [
            false, true, true
        ]
      },
      {
        'OR': [
            true, false, false, {
                'AND': [true, true]
            }
        ]
      },
      true
  ]
};

等同于((false&& true&& true)||(true || false || false ||(true&& true))|| true)

我们需要编写一个计算该值的函数

We need to write a function that calculates this

方法:

进入最内层并首先进行评估,然后移至顶部

Go to the innermost level and evaluate it first, moving to the top

var expressionEvaluator = function(opArr){
            var hasChildObjects = function(arr){
                if(Array.isArray(arr)){
                    return arr.some(function(obj){
                        return typeof(obj) === 'object';
                    });
                }
                else if(typeof(arr) === 'object'){
                    return true;
                }
            };
            var evaluateArr = function(list, operator){
                var result;
                if(operator === 'AND'){
                    result = true;
                    for(var i = 0; i<list.length; i++){
                        if(!list[i]){
                            result = false;
                        }
                    }
                }
                else if(operator === 'OR'){
                    result = false;
                    for(var i = 0; i<list.length; i++){
                        if(list[i]){
                            result = true;
                        }
                    }
                }
                return result;
            };
            var iterate = function(opArr){
                Object.keys(opArr).forEach(function(k){
                    if(hasChildObjects(opArr[k])){
                        iterate(opArr[k]);
                    }
                    else{
                        opArr = evaluateArr(opArr[k], k);
                    }
                });
            };
            iterate(opArr);
            return result;
        }

我能够到达最里面的对象并对其进行评估,但无法回到最顶层并评估整个表达式对象.

I am able to reach the innermost object and evaluate it but not reach back to the topmost level and evaluate the entire expression object.

推荐答案

您可以使用简单的递归函数.

You could use a simple recursive function.

  • 如果当前对象具有 OR 键,则检查
  • If the current object has OR key, then check if some of the items in the array are truthy.
  • If AND, check if every item is truthy.
  • If one of the item in the array is an object, recursively call the function on the object to get its value

const input={OR:[{AND:[false,true,true]},{OR:[true,false,false,{AND:[true,true]}]},true]};

function evaluate({ OR, AND }) {
  if (OR)
    return OR.some(c => typeof c === 'object' ? evaluate(c) : c)
  if (AND)
    return AND.every(c => typeof c === 'object' ? evaluate(c) : c)
}

console.log(evaluate(input))

由于回调函数相同,因此您也可以将操作获取到变量并动态调用:

Since the callback functions are same, you could also get the operation to a variable and call it dynamically:

function evaluate({ OR, AND }) {
  const array = OR ?? AND,
        operation = OR ? 'some' : 'every';
  
  return array[operation](c => typeof c === 'object' ? evaluate(c) : c)
}

OR

const evaluate = ({ OR, AND }) => OR ? OR.some(callback) : AND.every(callback),
      callback = c => typeof c === 'object' ? evaluate(c) : c

这篇关于用Javascript评估表达式树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆