What's wrong with this code js?

2

The first part of the code separates the URL into /

This is the current URL

  

www.mydomain.com/category/action

var url_atual = decodeURI(window.location.href);
var replace_url = url_atual.replace('http://www.meudominio.com/', '');
var split_url = replace_url.split('/'); 

Now split_url is an array with two values categoria and ação , the function below searches for that category in another object:

var val = split_url[1];
var data = Object.values(livros).filter(function(objecto) { 
    return objecto.categoria.toLowerCase().indexOf(val) > -1 
});

But data is not returning results, if I manually set val = "ação" the search finds all related results, but when it is passed through the URL, results are not found.

Note: This problem only occurs when there are special characters in the URL if I change the category to something like adventure or romance results, but when there is any special character or uppercase letter in the URL it returns an empty string.

    
asked by anonymous 23.12.2016 / 14:10

3 answers

2

It's hard to be sure when we can not simulate here, your return should not be: return objecto.categoria.toLowerCase().indexOf(val.toLowerCase()) > -1 comparing both in lower case.

    
23.12.2016 / 14:18
1

One option is to remove the accent before scanning. With ES6 would read:

var str = "Ação"
str = str.normalize('NFD').replace(/[\u0300-\u036f]/g,"");
console.log(str);
    
23.12.2016 / 14:19
0

To get the value of the action, make it easier

var url_atual = decodeURI(window.location.href);
var split_url = url_atual.split('/'); 
val = split_url[split_url.length -1]
  

[val="action"]

    
23.12.2016 / 14:23