My simple app has:
- data: array of objects
- users, setUsers: useState hooks
- filters: array of strings
- filteredUsers: it should be an array of objects;
In my app, a user can click and add a string to an array of filters (filters changing), then I'd like to create a new arr of objs filteredUsers, send it to users (by setUsers(filteredUsers)) and render a new list, based on filters.
When I'm using a single string, e.g. getFilteredUsers('string') it works, but I can't make it with an array.
const users = [
{
id: 849,
name: 'John',
city: 'London',
languages: ['English']
},
{
id: 294,
name: 'Ola',
city: 'Stockholm',
languages: ['Swedish', 'English']
},
{
id: 585,
name: 'Stefano',
city: 'Naples',
languages: ['Italian', 'French']
},
{
id: 946,
name: 'Anna',
city: 'Paris',
languages: ['French']
}
]
const four = document.querySelector('.four');
const arrayFilter = arr => {
const newUsers = filters.map(filter => getUsers(filter));
four.textContent = JSON.stringify(newUsers);
return newUsers
}
const test = ['French'];
console.log(arrayFilter(test))
I've also tried to use useEffect, but with no results.
jsfiddle: https://jsfiddle.net/gmp5doqv/165/
GitHub: https://github.com/lukaasz555/job-list/blob/main/components/Main/Main.tsx
arrayFilterfunction you're not actually doing anything with thearrparameter you're passing in. Also, in that function when you dofilters.map,filtersis referencing the value set above which isconst filters = ['French', 'Naples'];const arrayFilter = filterArray => { const newUsers = filterArray.map(filter => getUsers(filter))[0]; four.textContent = JSON.stringify(newUsers); return newUsers } const filterArray = ['French']; console.log(arrayFilter(filterArray))