Why doesn’t this function work in Mobile Browser?

Asked

Viewed 75 times

1

I’m using the code below to search for data in a json, and it works perfectly in Chrome and Firefox, but in mobile browser it doesn’t work, and just doesn’t return anything:

function searchTitles(e){
    var val = decodeURI(e);

    var data = Object.values(meu_json).filter(function(objecto) { 
        return objecto.titulo.toLowerCase().indexOf(val.toLowerCase()) > -1 
    });
    if(data == ""){
        var data = Object.values(meu_json).filter(function(objecto) { 
            return objecto.categoria.toLowerCase().indexOf(val.toLowerCase()) > -1 
        });
    }
    if(data != ""){
        alert(data);
    }else {
        alert('error');
    }   
}

When using in Chrome or firefox it returns the data and data != "" but the same in a mobile browser, it doesn’t work, just nothing happens, no error message.

  • I believe the problem lies in Object.values not compatible with most browsers. Try changing by for..in or a for classic. Docs for compatibility purposes.

  • Can you make an example with for in to see what @Lucascosta looks like

  • I answered with an example @Leoletto.

1 answer

0


Maybe the problem is Object.values, you can try replacing by for. in:

var valores = [];
for (property in meu_json){
  valores.push(meu_json[property]);
}

var data = valores.filter(function(objecto) { 
    return objecto.titulo.toLowerCase().indexOf(val.toLowerCase()) > -1 
});
  • It worked very well for me this way, there is some speed difference between it and Object.values?

  • 1

    Can you create a test on jsPerf @Leoletto. Since I don’t have your JSON, create one there =]

  • 1

    Got it, thanks for the tip, I’ll test it there later :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.