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
2
  • 3
    eval(data.join('')) although there is security risk associated to it. please refer : developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…! Commented Jun 16, 2020 at 6:09
  • What math operations do you consider normal? addition, subtraction, multiplication, divsion and modulus? +, - * / % Commented Jun 16, 2020 at 6:47

1 Answer 1

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('')));

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.