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));
Share:
15,799
Airikr
Author by

Airikr

Updated on June 07, 2022

Comments

  • Airikr
    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 from 63.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
      Airikr about 12 years
      I don't know exactly with it prints 63.00425720214844.
    • kpotehin
      kpotehin about 12 years
    • Airikr
      Airikr about 12 years
      I have already readed that question and also the answers.
    • Rob
      Rob about 12 years
      by 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
      Felix Kling about 12 years
      What does console.log(typeof s) or console.dir(s) tell you?
  • Airikr
    Airikr about 12 years
    Many thanks - it worked! :D I'll accept your answer as soon as I can.
  • Airikr
    Airikr about 12 years
    Thanks but Marc's answer was so much simpler :)
  • Felix Kling
    Felix Kling about 12 years
    If it's a number, you can also use s.toFixed(0).
  • Guffa
    Guffa about 12 years
    @ErikEdgren: Simpler in what way? Because parseInt is two characters less than Math.floor...?
  • Airikr
    Airikr about 12 years
    @Guffa: Because it's only one word :) Math.floor is very simple too though
  • Guffa
    Guffa about 12 years
    @ErikEdgren: Actually Math.floor(s) is simpler than parseInt(s.toString()), which is what it really should read...