0
const rlsts_1 = [1,2,3];
const rlsts_2 = [1,2,3];
const rlsts_3 = [1,2,3];

const a = math.matrix([rlsts_1,rlsts_2,rlsts_3]);

const c = (math.row(a,0))

c.forEach((num1, index) => {
    var formula = "var e_"+[index]+" = math.evaluate(['f = "+[num1]+"','g = 100', 'f * g']) " 
    var solution = "console.log(e_"+[index]+"[2])"
    
    var parsedformula = formula.replace(/e_0,/i, "e_");
    var parsedsolution = solution.replace(/e_0,/i, "e_");
    var Firstportion = parsedformula;
    var Secondportion = parsedsolution;
    
    const Combind = Firstportion.concat(Secondportion);
     
    console.log(Combind);     
    //eval(Combind); 

所以我正在使用 MathJS 库。然后包含了三个数组。然后我使用 MathJS 库放入一个矩阵。

我从矩阵中选择了一行。然后我使用包含索引的 for each 循环运行math.evaluateMath JS 并为每个实例指定一个唯一名称并从行中获取一个值。MathJS 文档说要引用对象,您可以索引math.evaluate表达式对象并索引到解决方案的第 3 行 ( f * g)。

因为行索引是二维的,所以我进行了字符串替换以将逗号从矩阵索引中取出,并且我使用行索引作为唯一标识符。

最后,我习惯concat在每次迭代中将公式和解决方案放在一起。所以我可以在每个迭代周期中记录表达式。

但是,当我尝试运行时,eval出现以下错误eval

发生异常:SyntaxError: Unexpected identifier

...没有堆栈跟踪。

4

0 回答 0