0

If my browser is on http://example/cars?type=ford, searchParams.get(type) returns null, and if it on http://example/cars?type=ford&type=dodge, it returns dodge. How can the first parameter after the question mark be included. I am operating Chrome Version 64.0.3282.167 (Official Build) (64-bit).

my.filter=function(type,value){
    var searchParams = new URLSearchParams(window.location.href);
    var search=searchParams.get(type);
    console.log(search)
    if(search) {
        search=search.split(',');
        if(!search.indexOf(value)) {
            search.push(value);
            searchParams.set(type,search.join(','));
        }
    }
    else searchParams.append(type,value);
    var url=searchParams.toString();
    console.log(url);
    //window.location.href = url;
}
user1032531
  • 24,767
  • 68
  • 217
  • 387

1 Answers1

0
!search.indexOf(value)

indexOf returns -1 (a true value) if there is no match, but 0 (a false value) if there is a match at position 0.

You can't test for falseness to see if there isn't a match!

Test against -1 instead.

search.indexOf(value) === -1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335