javascript 对象没有方法“indexOf”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10133318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:47:59  来源:igfitidea点击:

Object has no method 'indexOf'

javascriptindexof

提问by Erik

I have the following function (taken from Elevation Service @ Google Maps API) which output for example 63.00425720214844when I click somewhere on the map I have created with Google Maps JavaScript API v3:

我有以下函数(取自Elevation Service @ Google Maps API),例如63.00425720214844当我单击使用Google Maps JavaScript API v3创建的地图上的某处时输出:

function getElevation(event) {
    var locations = [];
    var clickedLocation = event.latLng;
    locations.push(clickedLocation);

    var positionalRequest = {
        'locations': locations
    }

    elevator.getElevationForLocations(positionalRequest, function(results, status) {
        if(status == google.maps.ElevationStatus.OK) {
            var s = results[0].elevation
            if(results[0]) {
                alert(s.substring(0, s.indexOf('.') - 1));
            } else {
                alert('Inget resultat hittades');
            }
        } else {
            alert('Det gick inte att hitta h?jdskillnaden p? grund av f?ljande: ' + status);
        }
    });
}

I want to remove everything after the dot including the dot, for example remove .00425720214844from 63.00425720214844but when I click somewhere on the map, I'm getting this error message in the console: Uncaught TypeError: Object 63.00425720214844 has no method 'indexOf'.

我想删除点之后的所有内容,包括点,例如 remove .00425720214844from63.00425720214844但是当我单击地图上的某个位置时,我在控制台中收到此错误消息:Uncaught TypeError: Object 63.00425720214844 has no method 'indexOf'

What have I done wrong?

我做错了什么?

Thanks in advance.

提前致谢。

回答by Guffa

The variable sdoesn't contain a string.

该变量s不包含字符串。

You can turn it into a string using:

您可以使用以下方法将其转换为字符串:

s = s.toString();

If it's a number, you can just use numeric functions instead:

如果它是一个数字,您可以只使用数字函数:

alert(Math.floor(s));

回答by Marc

Just do a javascript parseInt(63.00425720214844)to get 63.

只需执行 javascriptparseInt(63.00425720214844)即可获得 63。