使用reduce方法处理数组元素

时间:2016-11-09 10:04:07

标签: javascript arrays

我有这个数组finalArr = [12,+,4,-,8,*,2];我想将数组减少为单个值,如下所示:var result = 12+4-8*2;

这就是我所做的:

var operators = {'+' : function (a,b) {return a + b},
                 '-' : function (a,b){return a - b},
                 '*' : function (a,b){return a * b},
                 '/' : function (a,b){return a / b}};

var opSymbols = ['+','-','*','/'];
finalArr = [12,+,4,-,8,*,2];
finalArr.reduce(function (acc,next,index,arr){
        for (var m = 0; m < opSymbols.length; m++) {
            var op = '';
            if (opSymbols[m] === arr[1]) {
                op = opSymbols[m];
                acc = operators[op](arr[0],arr[2]);
            }
            if (index > 1 && opSymbols[m] === arr[index]) {
                op = opSymbols[m];
                acc +=  arr[index+1];
            }
        }

        return  acc;
    });

我在finalArr.reduce行中遇到语法错误(SyntaxError:expected expression,got',')我不认为我正在执行reduce方法。任何帮助?感谢

5 个答案:

答案 0 :(得分:1)

运算符是字符串,因此您需要引用它们:

finalArr = [12,'+',4,'-',8,'*',2];

答案 1 :(得分:1)

没有evalfunction calculate(a) { while (a.length > 1) { precedence.some(function (b) { return a.some(function (c, i) { if (b === c) { return operators[b](a, i); } }); }); } return a[0]; } var operators = { '+': function (a, i) { a[i - 1] += a.splice(i, 2)[1]; return true; }, '-': function (a, i) { a[i - 1] -= a.splice(i, 2)[1]; return true; }, '*': function (a, i) { a[i - 1] *= a.splice(i, 2)[1]; return true; }, '(': function (a, i) { var j = i + 1, b; while (j < a.length) { if (a[j] === ')') { b = a.splice(i + 1, j - i); b.pop(); a[i] = calculate(b); return true; } if (a[j] === '(') { return false; } j++; } }, ')': 0 }, precedence = ['(', '*', '+', '-']; console.log(calculate([12, '+', 4, '-', 8, '*', 2])); console.log(calculate([7, '*', 8, '-', 3.5, '*', 4]));的解决方案,但具有运算符优先级。

sheet['F50'] = 'Hello'
enter code here

答案 2 :(得分:0)

Eval is evil
但如果你想测试它:

finalArr = [12, '+', 4, '-', 8, '*', 2];
console.log(
  eval(finalArr.join(''))
)

答案 3 :(得分:0)

如果您的数组是一个混合字符串/ int数组,如

var finalArr = [12,'+',4,'-',8,'*',2];

您可以通过以下命令生成预期结果

var result = finalArr.join('');

如果你需要计算结果

var final = eval(result);

希望它能帮助你;)

答案 4 :(得分:0)

如果您真的想使用reduce而不使用eval

var opSymbols = ['+','-','*','/'];
var finalArr = [12,'+',4,'-',8,'*',2];
var result = finalArr.reduce(function (previousValue,currentValue,currentIndex,array){
    var res;
    var opr;
    if(typeof currentValue == 'number') {
            switch(previousValue.operator){
                case '+':
                    res = previousValue.value + currentValue;break;
                case '-':
                    res = previousValue.value - currentValue;break;
                case '*':
                    res = previousValue.value * currentValue; break;
                case '/':
                    res = previousValue.value / currentValue; break;
            }
            opr = previousValue.operator;
    }

    else if(typeof currentValue == 'string') {
        res = previousValue.value;
        opr = currentValue;
    }

    return {'value':res,'operator': opr};



},{'value':0,'operator':'+'});

console.log(result.value); // this gives 16