javascript - 为什么从数组中获取值时会出现 NaN(不是数字)错误?

标签 javascript arrays nan

我正在使用三 Angular 测量计算用户的位置。

如果我使用数组值,它会输出 NaN NaN ,但是如果我对值进行硬编码,它会按预期正常工作并输出

从数组中获取值:

    var beaconCoordinates = [[10,20], [200,300], [50,500]];

    //get values from array
    var aX = parseInt(beaconCoordinates[0,0]);
    var aY = parseInt(beaconCoordinates[0,1]);
    var bX = parseInt(beaconCoordinates[1,0]);
    var bY = parseInt(beaconCoordinates[1,1]);
    var cX = parseInt(beaconCoordinates[2,0]);
    var cY = parseInt(beaconCoordinates[2,1]);

对值进行硬编码:

    var aX = 2;
    var aY = 4;
    var bX = 5.5;
    var bY = 13;
    var cX = 11.5;
    var cY = 2;

这是其余的代码:

    var dA = 5.7;
    var dB = 6.8;
    var dC = 6.4;

    //trilateration / triangulation formula
    var S = parseInt((Math.pow(cX, 2.) - Math.pow(bX, 2.) + Math.pow(cY, 2.) - Math.pow(bY, 2.) + Math.pow(dB, 2.) - Math.pow(dC, 2.)) / 2.0);
    var T = parseInt((Math.pow(aX, 2.) - Math.pow(bX, 2.) + Math.pow(aY, 2.) - Math.pow(bY, 2.) + Math.pow(dB, 2.) - Math.pow(dA, 2.)) / 2.0);
    var y = ((T * (bX - cX)) - (S * (bX - aX))) / (((aY - bY) * (bX - cX)) - ((cY - bY) * (bX - aX)));
    var x = ((y * (aY - bY)) - T) / (bX - aX);

    //x and y position of user
    console.log(x,y);

有人可以向我解释一下吗?我很困惑。

最佳答案

访问数组的方式存在轻微错误。你需要

parseInt(beaconCoordinates[0][0]);

而不是

parseInt(beaconCooperatives[0,0]);

关于javascript - 为什么从数组中获取值时会出现 NaN(不是数字)错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27565359/

相关文章:

c++ - 确定NaN(不是数字值)

javascript - 在多个元素上使用 addEventListener,避免在未找到特定元素时出现 TypeError

php - 将数组转换为另一个数组

javascript - 无法使用node.insertBefore()方法注入(inject)DOM元素

r - 使用na.rm = TRUE时会删除NaN

在 R 中的数据集中用零替换 -inf、NaN 和 NA 值

javascript - 使用 var 设置 style.listStyleImage

javascript - 我收到 404 未找到 api

javascript - window.open 有时无法打开弹出窗口吗?

c# - 为什么 Array 类不直接公开其索引器?