Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript将int视为字符串中间公式_Javascript_Typescript - Fatal编程技术网

Javascript将int视为字符串中间公式

Javascript将int视为字符串中间公式,javascript,typescript,Javascript,Typescript,我有一个相当简单的javascipt公式: Math.round(minMax[0] + ((minMax[1] - minMax[0]) / 2)); 最小值[0]的值为1,最小值[1]的值为5 但出于某种原因,我从中得到了12个结果。在我期待3的地方,现在我猜测它计算minMax[1]-minMax[0]/2到2,然后将它们作为字符串相加,结果为1+2=12 我该怎么做才能让javascript将其视为数字 更新: 如果我把公式改成下面的公式,那么它是有效的,但是非常难看 index =

我有一个相当简单的javascipt公式:

Math.round(minMax[0] + ((minMax[1] - minMax[0]) / 2));
最小值[0]的值为1,最小值[1]的值为5

但出于某种原因,我从中得到了12个结果。在我期待3的地方,现在我猜测它计算minMax[1]-minMax[0]/2到2,然后将它们作为字符串相加,结果为1+2=12

我该怎么做才能让javascript将其视为数字

更新: 如果我把公式改成下面的公式,那么它是有效的,但是非常难看

index = Math.round(parseInt(minMax[0]) + ((parseInt(minMax[1]) - parseInt(minMax[0])) / 2));
更新2:如何找到最小最大值,这是在Typescript中

private getMinMaxIndex(parentIds: number[]): number[] {
            var minIndex = 99999999;
            var maxIndex = -1;

            for (var k in parentIds) {
                var index = this.getIndexOf(parentIds[k]);
                if (index > maxIndex) maxIndex = index;
                if (index < minIndex) minIndex = index;
            }
            return [minIndex, maxIndex];
        }
...
var minMax = this.getMinMaxIndex(limitedParents);
index = Math.round(parseInt(minMax[0]) + ((parseInt(minMax[1]) - parseInt(minMax[0])) / 2));

正如其他人指出的,我已经知道,变量不是整数。 我使用的是typescript,所以我没有考虑这个问题,为了解决这个问题,我在数字周围添加了parseInt,以确保它是一个整数


最重要的是,minMax[1]-minMax[0]/2是作为一个数字计算的,但minMax[0]+minMax[1]-minMax[0]/2不是。我只能猜测/2会强制javascript将变量视为整数,只是连接到minMax[0]+。

我刚刚将您的代码粘贴到Chrome的控制台中,结果是3。在Firefox中,也可以按预期工作。您在哪里执行此操作?您发布的代码并不像您所说的那样工作。这真的是导致您出现问题的代码吗?有一个强烈的迹象表明数组包含字符串,与您在此处发布的内容不太一样。@adeneo minMax[0]:string minMax[1]:string这是一个很好的提示,但遗憾的是,这不是问题的答案。
private getIndexOf(id: number) {
    var nodes: INode[] = this.nodes.Get();
    for (var i in nodes) {
        if (nodes[i].GetId() == id)
            return i;
    }

    console.log("Unable to find parent index, for node id: " + id + " in swimlane " + this.Name + ", this should not happen and will break.");
    console.log(nodes);
    return null;
}
export interface INode{
    GetId(): number;
    ...
}

export class SubmissionNode implements INode {

    ...

    constructor(data: DiagramInputObject) {
        this.data = data;
    }

    public GetId(): number {
        return this.data.Id;
    }
}

export interface DiagramInputObject {
    Id: number;
    ...
}

Finally the DiagramInputObject in question that is how it is parsed into the code
{
    Id: 1014, 
    UserId: 1, 
    Type: 0, 
    SwimLaneId: null, 
    SwimLaneName: null, 
    Submitted: new Date("3/9/2014 8:56:00 PM"), 
    ParentId: 9, 
    MergeId: null, 
    Exportable: false, 
    TypeData: "null", 
}
private getIndexOf(id: number) {
    var nodes: INode[] = this.nodes.Get();
    for (var i in nodes) { // <---- PROBLEM HERE
        if (nodes[i].GetId() == id)
            return i;
    }

    console.log("Unable to find parent index, for node id: " + id + " in swimlane " + this.Name + ", this should not happen and will break.");
    console.log(nodes);
    return null;
}
for(var i = 0; i < nodes.length; i++) {
    /* same loop body */
}