1

I am getting an array value after an operation.

let data = formula.map(a => typeof a === 'number' ? result[index++] : a)

My output that I am getting is

data=["6.9","+","7.1","-","3.0"]

I wanted to perform normal math operation on this data like 6.9+7.1-3.0 so my end output will be

endresult = 11
shakz
  • 629
  • 3
  • 14
  • 38
  • 3
    `eval(data.join(''))` although there is security risk associated to it. please refer : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval#Never_use_eval! – gorak Jun 16 '20 at 06:09
  • What math operations do you consider normal? addition, subtraction, multiplication, divsion and modulus? `+, - * / %` – apena Jun 16 '20 at 06:47

1 Answers1

2

You can use eval and pass a string into it. To create a string from the array you can use join().

var data=["6.9","+","7.1","-","3.0"];

var data1=["6.9","+","7.1","*","3.0"];

console.log(eval(data.join('')));
console.log(eval(data1.join('')));
Rajan
  • 1,512
  • 2
  • 14
  • 18