Chrome Extension

Javascript ES6 Array Filter

const allUsers = [{
        "name": "Zediane Araújo",
        "age": 66,
        "picture": "https://randomuser.me/api/portraits/thumb/women/13.jpg",
        "gender": "female"
    },
    {
        "name": "Nara Porto",
        "age": 70,
        "picture": "https://randomuser.me/api/portraits/thumb/women/77.jpg",
        "gender": "female"
    },
    {
        "name": "Ferafina da Cruz",
        "age": 66,
        "picture": "https://randomuser.me/api/portraits/thumb/women/3.jpg",
        "gender": "female"
    },
    {
        "name": "Cliseu Castro",
        "age": 48,
        "picture": "https://randomuser.me/api/portraits/thumb/men/24.jpg",
        "gender": "male"
    },
    {
        "name": "Aiara Cavalcanti",
        "age": 69,
        "picture": "https://randomuser.me/api/portraits/thumb/women/3.jpg",
        "gender": "female"
    }
];

const filteredUsers = allUsers.filter((user) => {
    const inputValue = "CLI"; //Obtenha valor do input com .value, deixei esse valor para exemplo 
    const valueFilterLower = inputValue.toLowerCase(); //convertendo valor do input para minisculo
    const lowername = user.name.toLowerCase(); // converta o nome atual para minisculo
    return lowername.indexOf(valueFilterLower) > -1; // O método indexOf() retorna o primeiro índice em que o elemento pode ser encontrado no array, retorna -1 caso o mesmo não esteja presente.
    //lembrando que O método filter() cria um novo array com todos os elementos que passaram no teste implementado pela função fornecida.
});

console.log(filteredUsers);
Output
[
{
"name": "Cliseu Castro",
"age": 48,
"picture": "https://randomuser.me/api/portraits/thumb/men/24.jpg",
"gender": "male"
}
]"

Search Index Data (The code snippet can also be found with below search text)

Javascript ES6 Filter array considering accents in words
Was this helpful?
0 Comments
Never leave your website again in search of code snippets by installing our chrome extension.
Open Code Manager