如何计算数组a里面如:a = [‘2‘, ‘*‘, ‘5‘]

it2025-01-18  1

如何计算数组a里面 如:a = [‘2’, ‘*’, ‘5’]

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> </body> <script> //如何计算数组a里面 如:a = ['2', '*', '5'] var a = ['2', '*', '5'] var xxx = eval((a + "").replace(/,/g, "")) // (a+"") 数组变字符串 // replace(/,/g,"") 将 ,变为 '' // eval 计算字符串 "a*b+c/d-e" console.log(a) console.log((a + "")) //字符串 console.log((a + "").replace(/,/g, "")) //去掉, console.log(xxx) //计算结果 </script> </html>

最新回复(0)