javascript
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"
}
]"
{
"name": "Cliseu Castro",
"age": 48,
"picture": "https://randomuser.me/api/portraits/thumb/men/24.jpg",
"gender": "male"
}
]"
Was this helpful?
Similar Posts
- Assign items of arrays to multiple variable when creating it (Array Destructuring) Javascript(ES6)
- Filter an Array using object key value in Javascript
- Javascript ES6 Spread Operator
- Javascript ES6 Rest Parameters
- Javascript ES6 Destructuring
- Javascript ES6 Template strings
- JavaScript ES6 filtering object with multiple params