I'm trying to filter posts by category for a blog that uses React. Here is what the data looks like:
const posts = [
{
"categories": [
{
"title": "tag1"
},
{
"title": "tag2"
},
{
"title": "tag3"
}
],
"title": "First post",
// more stuff here
},
{
"categories": [
{
"title": "tag2"
},
{
"title": "tag3"
},
{
"title": "tag4"
},
{
"title": "tag5"
}
],
"title": "Second post"
// more stuff here
},
{
"categories": [
{
"title": "tag1"
},
{
"title": "tag3"
},
{
"title": "tag4"
}
],
"title": "Third post"
// more stuff here
},
{
"categories": [
{
"title": "tag1"
},
{
"title": "tag2"
},
{
"title": "tag4"
},
{
"title": "tag5"
}
],
"title": "Fourth post"
// more stuff here
}
]
I have a piece of state called filter that updated from a <select> menu, which looks like this:
const [filter, setFilter] = useState('all');
How would I write the rest of this code block?
useEffect(() => {
if (filter !== 'all') {
// Not sure what to do here
}
}, [])
Not sure if I use filter on the first array, or do I keep using filter all the way down? I've tried a bunch of things, but haven't been able to get it to work.