Search with case insensitive

Talk guys good afternoon, I have a search field but I need it to return the values regardless of toLowerCase() and toUpperCase()

Ex ball, I can search like ball or else ball

I have the following line of Code:

function filterMaterialList(query){
    let categorySelect = document.querySelector('select[name=category]');
    let materialList = materials[categorySelect.value];
    let reducedList = [];

    if(query === ''){
        return materialList;
    }

    for(let material of materialList){
        if(material.name.toLowerCase().indexOf(query) != -1){
            reducedList.push(material);
        }
    }

    if(reducedList.length == 0){
        reducedList.push({name: 'Nenhum resultado encontrado!', id: null});
    }

    return reducedList;
}

But how am I using:

if(material.name.toLowerCase().indexOf(query) != -1){... I only succeed when I search for ball

I need this function to be case insensitive, can anyone help me ?

Author: Raphael Melo De Lima, 2018-10-23

2 answers

The simplest / practical way is to also pass the query string to the same format:

if (material.name.toLowerCase().indexOf(query.toLowerCase()) != -1)
 6
Author: rLinhares, 2018-10-23 17:22:26

Being the parameter "query" your search criterion, follows suggestion: just put the query.toLowerCase(), in this way, regardless of how the search criterion is informed, both will be compared with Lower-Case.

function filterMaterialList(query){
    let categorySelect = document.querySelector('select[name=category]');
    let materialList = materials[categorySelect.value];
    let reducedList = [];

    if(query === ''){
        return materialList;
    }

    for(let material of materialList){
        if(material.name.toLowerCase().indexOf(query.toLowerCase()) != -1){
            reducedList.push(material);
        }
    }

    if(reducedList.length == 0){
        reducedList.push({name: 'Nenhum resultado encontrado!', id: null});
    }

    return reducedList;
}
 7
Author: Adjair Costa, 2018-10-23 18:36:06