Object has no method 'indexOf'
15,799
Solution 1
Just do a javascript parseInt(63.00425720214844)
to get 63.
Solution 2
The variable s
doesn't contain a string.
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));
Author by
Airikr
Updated on June 07, 2022Comments
-
Airikr almost 2 years
I have the following function (taken from Elevation Service @ Google Maps API) which output for example
63.00425720214844
when I click somewhere on the map I have created with 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
.00425720214844
from63.00425720214844
but 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'
.What have I done wrong?
Thanks in advance.
-
Airikr about 12 yearsI don't know exactly with it prints
63.00425720214844
. -
kpotehin about 12 years
-
Airikr about 12 yearsI have already readed that question and also the answers.
-
Rob about 12 yearsby the way, you might consider moving the s = ... assignment down by 1 line, otherwise you're checking for results[0] after you've already accessed it.
-
Felix Kling about 12 yearsWhat does
console.log(typeof s)
orconsole.dir(s)
tell you?
-
-
Airikr about 12 yearsMany thanks - it worked! :D I'll accept your answer as soon as I can.
-
Airikr about 12 yearsThanks but Marc's answer was so much simpler :)
-
Felix Kling about 12 yearsIf it's a number, you can also use
s.toFixed(0)
. -
Guffa about 12 years@ErikEdgren: Simpler in what way? Because
parseInt
is two characters less thanMath.floor
...? -
Airikr about 12 years@Guffa: Because it's only one word :)
Math.floor
is very simple too though -
Guffa about 12 years@ErikEdgren: Actually
Math.floor(s)
is simpler thanparseInt(s.toString())
, which is what it really should read...