简体   繁体   中英

I don't understand this substring error

I noticed FireFox reports an error in my code, but can't understand what is wrong.

[13:36:02.868] TypeError: arr[i][0].substring is not a function @ file:///home/asdf/Desktop/app/dic.js:10

Which seems to point to the ss variable line. AFAIK, I'm using it correctly (ie on the end of a string). Here the code snippet...

// quick array example...
var arr = [
["tammikuuta", "January"], 
["helmikuuta", "February"], 
["maaliskuuta", "March"]
]; 

function userInput(val){

var result = document.getElementById('result');
result.innerHTML = '';

if(val && val.length > 2){
    for(var i = 0; i < arr.length; i++){
        var ss = arr[i][0].substring(0,val.length); // ss (SubString) checks the number of characters currently typed 
        if(ss.toLowerCase() !== val.toLowerCase()){ // check substring against the user input
            continue;
        }
        else { // display the output...
            var res = arr[i][1]; 
            var reg = eval('/'+val+'/i');
            var found = arr[i][0].replace(reg, '<span class="r">$&<\/span>');
            if(result.innerHTML.length > 0){
                result.innerHTML += ',<br />';
            }
            result.innerHTML += found + '<b>' + ' > ' + '</b>' + '<span class="g">' + res + '<\/span>';
        }
    }
}
else {return;}
}

Please advice?

Edited: More code requested.

try this

 var ss = arr[i][0].toString()
ss = ss.substring(0,val.length);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM