通过递归在对象中循环更深

时间:2019-01-30 12:59:15

标签: javascript json object recursion

我尝试将对象更深地循环,因为该对象具有树并且非常深,才能使其循环并获取数据,我应该尝试对其进行递归,将其卡在此处,结果为{{1} }

这是数据,输出在其中:

undefined

我尝试在最后一个条件function operationEvaluator(operation) { Object.keys(operation).forEach(el => { if(typeof operation[el] === 'object'){ return operationEvaluator(operation[el]) } if(typeof operation[el] === 'number'){ return operation.left + operationEvaluator(operation.op) } else { if(operation.op == '-'){ return operation.left - operation.right.left } else if( operation.op == '*'){ // console.log(operation.left*operation.right.left); return operation.left * operation.right.left } else if(operation.op == '+' ){ return operation.left + operation.right.left } else if(operation.op == '/' ){ return operation.left / operation.right.left } } }) } var op1 = { left: 5, op: '-', right: { left: 3, op: '*', right: { left: 8, op: '-', right: { left: 200, op: '/', right: 5, } } } }; // prosses: 5 - (3 * (8 - (200 / 5))) console.log(operationEvaluator(op1)); // 101 var op2 = { left: { left: 10, op: '*', right: { left: 2, op: '+', right: 1, }, }, op: '+', right: { left: 5, op: '*', right: { left: 1, op: '-', right: { left: 1, op: '+', right: 2, } } } }; // prosses: ((10 * (2 + 1)) + (5 * (1 - (1 + 2))) console.log(operationEvaluator(op2)); // 20console.log的每个数据,它显示了elseoperation.left的数量 但是当我返回它时,结果是不确定的,没什么可炫耀的

我错过了什么吗? 其他条件下的示例

IF操作等于operations.right.left  然后我console.log operation.left和operation.right.left 它显示了数字,我尝试将其乘以console.log,显示了结果,

4 个答案:

答案 0 :(得分:2)

您可以在compare_intleftright部分中使用destructuring assignment,并为运算符及其功能使用辅助对象。

然后检查opleft操作数是否是对象,然后使用该对象对函数进行递归调用或仅移交值。

right

答案 1 :(得分:1)

您的代码固定如下:

function operationEvaluator(operation) {
    let computedRightTerm;
    let computedLeftTerm;
    if(typeof operation.right === 'number') {
      computedRightTerm = operation.right;
    }
    else {
      computedRightTerm = operationEvaluator(operation.right);
    }
    if(typeof operation.left=== 'number') {
      computedLeftTerm= operation.left;
    }
    else {
      computedLeftTerm= operationEvaluator(operation.left);
    }
    if(operation.op == '-'){
      return computedLeftTerm - computedRightTerm;
    } else if( operation.op == '*'){
      // console.log(operation.left*operation.right.left);
      return computedLeftTerm * computedRightTerm;
    } else if(operation.op == '+' ){
      return computedLeftTerm + computedRightTerm;
    } else if(operation.op == '/'  ){
      return computedLeftTerm / computedRightTerm;
    }
}


var op1 = {
  left: 5,
  op: '-',
  right: {
    left: 3,
    op: '*',
    right: {
      left: 8,
      op: '-',
      right: {
        left: 200,
        op: '/',
        right: 5,
      }
    }
  }
};

// prosses: 5 - (3 * (8 - (200 / 5)))
console.log(operationEvaluator(op1)); // 101

var op2 = {
  left: {
    left: 10,
    op: '*',
    right: {
      left: 2,
      op: '+',
      right: 1,
    },
  },
  op: '+',
  right: {
    left: 5,
    op: '*',
    right: {
      left: 1,
      op: '-',
      right: {
        left: 1,
        op: '+',
        right: 2,
      }
    }
  }
};

// prosses: ((10 * (2 + 1)) + (5 * (1 - (1 + 2)))
console.log(operationEvaluator(op2)); // 20

答案 2 :(得分:0)

我对操作员使用<div><button type="button" class="btn btn-primary btn-large" data-text="Lorem">Lorem</button></div> <div><button type="button" class="btn btn-primary btn-large" data-text="Lorem Ipsum">Lorem Ipsum</button></div> <div><button type="button" class="btn btn-primary btn-large" data-text="Lorem Ipsum Dolor">Lorem Ipsum Dolor</button></div>语句来略微重写您的代码:

switch

答案 3 :(得分:0)

让它尽可能简单。 eval接受表达式e-

const eval = e =>
  // if the expression is an object ...
  Object (e) === e
    // apply the operator to the evaluated operands ...
    ? apply (e.op, eval (e.left), eval (e.right))
    // otherwise the expression is already a primitive
    : e

apply也很简单-

const env =
  { '+': (a, b) => a + b
  , '-': (a, b) => a - b
  , '*': (a, b) => a * b
  , '/': (a, b) => a / b
  }

const apply = (f, ...args) =>
{ if (env[f] === undefined)
    throw Error (`unknown operator: ${f}`)
  else
    return env[f] (...args)
}

尝试看看-

eval (op1)
// 101

eval (op2)
// 20

eval ({ left: 10, op: '^', right: 2 })
// unknown operator: ^

展开以下代码段,以在您自己的浏览器中验证结果-

const env =
  { '+': (a, b) => a + b
  , '-': (a, b) => a - b
  , '*': (a, b) => a * b
  , '/': (a, b) => a / b
  }

const eval = e =>
  Object (e) === e
    ? apply (e.op, eval (e.left), eval (e.right))
    : e
  
const apply = (f, ...args) =>
{ if (env[f] === undefined)
    throw Error (`unknown operator: ${f}`)
  else
    return env[f] (...args)
}

const op1 =
  { left: 5, op: '-', right: { left: 3, op: '*', right: { left: 8, op: '-', right: { left: 200, op: '/', right: 5 } } } }

const op2 =
  { left: { left: 10, op: '*', right: { left: 2, op: '+', right: 1, }, }, op: '+', right: { left: 5, op: '*', right: { left: 1, op: '-', right: { left: 1, op: '+', right: 2 } } } }

console .log
  ( eval (op1) // 101
  , eval (op2) // 20
  )