6

i've got two devices one with Lollipop and one with Kitekat... the one with Lollipop does not report any error but when i try my application i obtain this error:

10-13 16:56:56.126: I/chromium(6322): [INFO:CONSOLE(99)] "Uncaught
TypeError: Object ALIM. IL SOLE DI LOPARDO MARIANGELA has no 
method 'startsWith'", source: file:///android_asset/www/soggetti3.html (99)

here is a part of my javascript:

 function onDeviceReady() {

        window.rancolor=ranColor();

        var ricerca=localStorage.getItem('testo_ricerca');
        var risultati = JSON.parse(localStorage["risultati"]);

        var ricerca1=ricerca.toString();
        ricerca1=ricerca1.toUpperCase();
        var res_match=[];

        var lll=risultati.length;

        for(var r=0;r<lll;r++){

            var ppp0=risultati[r][1].toString();
            var ppp1=risultati[r][0].toString();
            var ppp2=risultati[r][2].toString();
            var ppp3=risultati[r][3].toString();

            ppp0=ppp0.toUpperCase();
            alert(ppp0);
            alert(ricerca1);
            var check=ppp0.startsWith(ricerca1);

            if(check==true){
                res_match=res_match.concat([[ppp1,ppp0,ppp2,ppp3]]);
            }

        }
        var y=res_match.length;

how should i search an array of strings searching the strings that begin with some other string?

Lorenzo
  • 673
  • 1
  • 11
  • 25

1 Answers1

13

The easiest way to implement the startWith function is shown below, then you can use startsWith in your code:

if (typeof String.prototype.startsWith != 'function') {
  // see below for better implementation!
  String.prototype.startsWith = function (str){
    return this.indexOf(str) === 0;
  };
}
Dilberted
  • 1,172
  • 10
  • 23
  • 1
    thanks man it works! but i don't understand the fact that for some device startsWith is defined and for Others not...the code was the same! – Lorenzo Oct 13 '15 at 15:57
  • 1
    Whether startsWith is available will depend on your browser engine... looks like the Android browser didn't implement this in JS but Chrome does see mobile tab here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith – Simon Prickett Oct 13 '15 at 17:19
  • 2
    Or simply use `str1.indexOf(str2) === 0` instead – Reverend Sfinks Jun 30 '16 at 09:25